import 后的组件,如果在代码里给它传数据?

2016-11-03 08:16:24 +08:00
 akaylh

比如有组件 C 和 D ,他们根据传入的数据不同,界面也不一样。 现在组件 B 里 import 组件 C 和 D ,请问如何在 js 代码里给 C , D 组件传递数据呢?


<template>
    <div>
     <component :is="currentView"></component>
   </div>
</template>

<script>

  import C from './C.vue'
  import D from './D.vue'

    export default {
      data() {
        return {
          currentView:null
        }
      },   
      methods: {
        clickSwitch(row, event){
          if(row.type==='c'){
            this.currentView = C; //这里如果给 C 组件传送数据呢?
          }else{
            this.currentView = D;//这里如果给 D 组件传送数据呢?
          }
        }
      }
    }
</script>
1882 次点击
所在节点    Vue.js
5 条回复
readonly
2016-11-03 08:31:09 +08:00
is 只接收字符串吧!那么属性就是写在 component 上面了
hxsf
2016-11-03 08:33:31 +08:00
v-if 可破
Troevil
2016-11-03 08:34:05 +08:00
component 内定义
props :{
xx:{ type:String }
}

, 然后 调用时 可用
< component :xx="aa" >
aa 可以为变量或者字符串等
Troevil
2016-11-03 08:34:37 +08:00
@Troevil 然后 component 内可以接收此参数
akaylh
2016-11-03 09:25:38 +08:00
@Troevil @hxsf 你们提供的方法都可以,非常感谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/317607

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX