Vue.js:如何在单个文件组件中指定道具?

新手上路,请多包涵

我正在定义一个 文件组件

我想在该组件上使用 props 选项。

但是我在哪里可以添加代码?

 <template>
  <div class="hello">
    <h1>{{ msg }}</h1>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // note: changing this line won't causes changes
      // with hot-reload because the reloaded component
      // preserves its current state and we are modifying
      // its initial state.
      msg: 'Hello World!'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1 {
  color: #42b983;
}
</style>

原文由 Alfred Huang 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 237
2 个回答

经过长时间的实验,我找到了一个实用的解决方案:

项目文件结构:

 src/
  assets/
  components/
    Home.vue
  App.vue
  main.js
package.json
config.js
index.html

现在,我们要访问根组件 App 子组件 Home.vue 内的 vm 字段,开启 vue-route

主.js:

 import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App'

Vue.use(VueRouter);

let router = new VueRouter();

router.map({
    '/': {
        name: 'home',
        component: require('./components/Home')
    }
});

router.start(App, 'body');

应用程序.vue:

 <template>

    <p>The current path: {{ $route.path }}.</p>
    <p>Outer-Value: {{ outer_var }}</p>
    <hr/>

    <!-- The below line is very very important -->
    <router-view :outer_var.sync="outer_var"></router-view>

</template>

<script>
    import Home from './compnents/Home.vue'

    export default {
        replace: false,
        components: { Home },
        data: function() {
            return {
                outer_var: 'Outer Var Init Value.'
            }
        }
    }
</script>

主页.vue

 <template>
    <div>
        <p><input v-model="outer_var" /></p>
        <p>Inner-Value: {{ outer_var }}</p>
    </div>
</template>

<script>
    export default {
        // relating to the attribute define in outer <router-view> tag.
        props: ['outer_var'],
        data: function () {
            return {
            };
        }
    }
</script>


结论

请注意,内部道具将属性绑定在组件标签的属性上(在这种情况下为 <router-view> 标签。),而 不是 直接在父组件上。

因此,我们 必须 手动将传递的 props 字段绑定为组件标签上的属性。见:http: //vuejs.org/guide/components.html#Passing-Data-with-Props

另外,请注意我在该属性上使用了 .sync ,因为默认情况下绑定是 单向的:http: //vuejs.org/guide/components.html#Prop-Binding-Types

可以看到,通过嵌套组件共享状态有点混乱。为了更好地实践,我们可以使用 Vuex

原文由 Alfred Huang 发布,翻译遵循 CC BY-SA 3.0 许可协议

你可以这样做:

应用程序.js

 <template>
  <div class="hello">
    <h1>{{ parentMsg }}</h1>
    <h1>{{ childMsg }}</h1>
  </div>
</template>

<script>
export default {
  props: ['parentMessage'],
  data () {
    return {
      childMessage: 'Child message'
    }
  }
}
</script>

<style scoped>
h1 {
  color: #42b983;
}
</style>

main.js

 import Vue from 'vue'
import App from './App.vue'

new Vue({
  el: '#app',
  data() {
    return {
      message: 'Parent message'
    }
  },
  render(h) {
    return h(App, { props: { parentMessage: this.message } })
  }
});

原文由 anthonygore 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题