vue 组件传值

1.父组件向子组件传值(props)

子组件重复调用,根据不同数值渲染不同内容

父组件
将设置的传值变量,值,绑定在子组件的标签上
在这里插入图片描述
在这里插入图片描述
子组件
子组件接收到传值名称,直接用就行了
在这里插入图片描述
在这里插入图片描述

2.子组件向父组件传值($emit)

子组件内部值父组件可直接调用

子组件
定义传的值,事件名

<el-button type="primary" plain @click="submit()">登录</el-button> 
<script>
export default {
  data(){
    return{
      input:'',
      word:'子组件数据' //定义所需传值
      set1:2,
      set2:3,
    }
  },
  methods:{
    submit(){
      this.$emit('getWord',this.word,this.set1,this.set2)  //('触发的事件名',传递参数)
    }
  }
}
</script>

父组件
接收事件,定义自己调用的方法

<el-header>
  <headerr @getWord='getwords'></headerr> //绑定到子组件方法
  /*多变量*/
   <headerr @getWord='getwords(arguments)'></headerr> //绑定到子组件方法
</el-header>

<script>
import headerr from "@/components/common/header";
export default {
  name: "index",
  data() {
    return {
      text:'',
      set_1:'',
      set_2:'',
     };
  },
  components:{headerr},
  methods: {
    getwords(data){  //父组件定义方法取值
      this.text=data;
    }
    /*多变量*/
     getwords(data){  //父组件定义方法取值
      this.text=data[0];
      this.set_1=data[1];
      this.set_2=data[2];
    }
  },
};
</script>

3. 路由传值

query

传参方式:
<router-link :to="{path:'/resultList',query:{word:scope.row.word}}">
<router-link :to="{name:'resultList',query:{word:scope.row.word}}">

接收:
this.$route.query.word

路径会带参数:
http://localhost:8080/resultList?word=11111

实例:表格带参数跳转详情页

//需跳转页面
<el-table-column prop="keywords" label="关键词" ></el-table-column>
<el-table-column prop="result" label="详情" width="110" align="center">
   <template slot-scope="scope">
   <router-link :to="{path:'/resultList',query:{word:scope.row.keywords}}">
    <el-button>详情</el-button>
   </router-link>
 </template>
</el-table-column>

//详情页面
<script>
created(){
   this.text=this.$route.query.word;
},
</script>

params

传参方式:
<router-link :to="{name:'resultList',params:{word:scope.row.word}}">

接收:
this.$route.params.word (页面刷新数据丢失)

路由无参数
http://localhost:8080/resultList
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值