Blogger Information
Blog 87
fans 1
comment 0
visits 58333
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
vue父子组件间属性、函数的相互调用
阿杰
Original
617 people have browsed it

1、父组件

  • 引入子组件
  1. <!-- 组件 -->
  2. <myTest></myTest>
  3. import myTest from "./components/myTest.vue"
  4. components:{
  5. myTest
  6. },
  7. data(){
  8. return {
  9. parentTitle:'父组件属性',
  10. }
  11. }
  12. },
  13. methods:{
  14. parentFun(){
  15. console.log('父组件方法');
  16. }
  17. }

2、子组件

  1. <template>
  2. <div>myTest页面</div>
  3. </template>
  4. data(){
  5. return {
  6. name:'子组件属性'
  7. }
  8. },
  9. methods:{
  10. childFun(){
  11. console.log('子组件方法');
  12. }
  13. }

3、父组件调用子组件属性与函数

  • 给子组件添加个ref属性
  1. <!-- 组件 -->
  2. <myTest ref="testComponent"></myTest>
  • 通过res来调用子组件属性与函数
  1. console.log(this.$refs.testComponent.name);
  2. this.$refs.testComponent.childFun();

4、子组件调用父组件属性与函数

  • 子组件里设置props属性来接受父组件属性
  1. props:["title"],
  • 父组件里绑定属性
  1. <!-- 组件 -->
  2. <myTest ref="testComponent" :title="parentTitle"></myTest>

  • 通过emit属性调用父组件函数
  1. this.$emit('parentM');

  • 或者通过$parent属性调用父组件函数
  1. this.$parent.parentFun();

5、关于路由传参的区别

  • params:/router1/:id ,/router1/123,/router1/789 ,这里的id叫做params
    query:/router1?id=123 ,/router1?id=456 ,这里的id叫做query。

  • 1:query方式传参和接收参数:

  • params方式传参和接收参数:

  • 2:query对应path,params对应name,path的参数会显示再地址栏,而params的参数不会显示再地址栏
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post