为了账号安全,请及时绑定邮箱和手机立即绑定

将变量向下传递给VueJS组件

将变量向下传递给VueJS组件

狐的传说 2021-05-02 16:21:20
我需要将变量传递给组件。我的设置是这样的:主:Meet.vue<html> <div class="carousel-cell">    <Category :searchTerm="woman"></Category>    </div> <div class="carousel-cell">    <Category :searchTerm="men"></Category>    </div>[..]<script>import Category from './Category'export default {  components: {    Category  },子类别:Category.vue:export default {  data () {    return {      search: [how to access the **searchTerm** here?]如何访问SEARCHTERM <Category :searchTerm="woman"></Category>从Meet.vue在Category.vue?
查看完整描述

3 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

调用Category组件时,请执行以下操作。


<Category :search="woman"></Category>


此处:search:"woman"表示将woman值作为值传递给Category组件的属性。


然后在Category组件中:


export default {

  props: {

    search: {type: string, required: true}

    //Here it will give error if the search is passed anything but string

    //And if search is not passed it will give error. 

  }

  //Everything else

或者


export default {

  props: [search], //No checking of data type and it's not requried

  //Everything else


查看完整回答
反对 回复 2021-05-13
?
桃花长相依

TA贡献1860条经验 获得超8个赞

您正在使用props,所以答案是:

export default{
    props: ['searchTerm'],

然后可以直接使用。


查看完整回答
反对 回复 2021-05-13
  • 3 回答
  • 0 关注
  • 212 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号