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

Alf*_*ang 30 javascript mvvm ecmascript-6 vue.js vue-component

我正在定义一个文件组件

我想在该组件上使用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>
Run Code Online (Sandbox Code Playgroud)

Alf*_*ang 25

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

项目文件结构:

src/
  assets/
  components/
    Home.vue
  App.vue
  main.js
package.json
config.js
index.html
Run Code Online (Sandbox Code Playgroud)

现在,我们想要访问根组件 - App子组件内的vm字段Home.vue,用vue-routeon.

main.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');
Run Code Online (Sandbox Code Playgroud)

App.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>
Run Code Online (Sandbox Code Playgroud)

Home.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>
Run Code Online (Sandbox Code Playgroud)

结论

请注意,内部prop在组件标记的属性上绑定了属性(<router-view>在本例中为Tag.),而不是直接在父组件上.

因此,我们必须手动绑定传递的props字段作为组件标记的属性.请参阅:http://vuejs.org/guide/components.html#Passing$-with-Props

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

你可以看到,通过嵌套组件共享状态有点困惑.为了更好地练习,我们可以使用Vuex.

  • @YerkoPalma,哈哈,你注意到这一点......事实上我想在这个标签上获得5分,然后创建一个同义词......一切都是针对闪亮的闪亮徽章..你介意给我一点意见吗?目标? (4认同)
  • 首选`vue.js`标签而不是'vuejs`标签,因为第一个问题接近1k问题,而最后一个问题甚至没有10个问题,实际上它应该被烧掉.你会得到更多的关注;) (2认同)

ant*_*ore 20

你可以这样做:

app.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>
Run Code Online (Sandbox Code Playgroud)

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 } })
  }
});
Run Code Online (Sandbox Code Playgroud)


Gij*_*rts 14

几个月前,Vue有自己的参考样式指南和类似的东西.道具是其中一个参考,实际上是必不可少的.

props: ['status']
Run Code Online (Sandbox Code Playgroud)

props: {
  status: String
}
Run Code Online (Sandbox Code Playgroud)

更好

props: {
  status: {
    type: String,
    required: true,
    validator: function (value) {
      return [
        'syncing',
        'synced',
        'version-conflict',
        'error'
      ].indexOf(value) !== -1
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到更多相关信息