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.
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)
| 归档时间: |
|
| 查看次数: |
23413 次 |
| 最近记录: |