【问题标题】:Vue.js: How to specify props in single file component?Vue.js:如何在单个文件组件中指定道具?
【发布时间】:2016-08-09 01:32:32
【问题描述】:

我正在定义一个single file 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>

【问题讨论】:

  • 查看下面的@Gijsberts 答案,其中包含有关 VueJS 样式指南以及如何键入道具的重要说明

标签: javascript mvvm ecmascript-6 vue.js vue-component


【解决方案1】:

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

项目文件结构:

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

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

ma​​in.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');

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>

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>

结论

注意,inner prop 将属性绑定在组件标签的属性上(本例中为&lt;router-view&gt;标签。),NOT直接在父组件上。

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

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

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

【讨论】:

  • 首选vue.js标签而不是vuejs标签,因为第一个问题接近1k,最后一个问题甚至没有10个问题,其实应该是烧焦。你会得到更多的关注;)
  • @YerkoPalma,哈哈,你注意到了这一点......事实上我想在这个标签上获得 5 分,然后创建一个同义词......一切都针对闪亮闪亮的徽章......会你介意给我一分吗?
【解决方案2】:

你可以这样做:

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>

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 } })
  }
});

【讨论】:

  • 这对我来说看起来像 typescript 语法,对吗?在普通的 JS 中看起来如何?例如require.
  • @NEXTLEVELSHIT 这对我来说是普通的 JS,但在 Vue 框架中。 es2015 可能通过 Babel。
【解决方案3】:

几个月前,Vue 有自己的styleguide 用于参考和类似的东西。道具是参考之一,实际上是必不可少的。

不好

props: ['status']

不错

props: {
  status: String
}

更好

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

您可以在此here 上找到更多信息

【讨论】:

  • 你甚至可以用includes代替indexOf(value) !== -1
猜你喜欢
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2017-08-17
  • 2022-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多