我正在关注此CodePen,并尝试将其放入默认的 VueJS 2.0 Boilerplate 中。这就是我将文件拆分的方式:
App.vue
main.js
components/Transitions.vue
components/Controls.vue
components/Page.vue
我在让它运行时遇到了很大的问题。例如我的 App.vue 没有找到state
. 这就是我在main.js中定义它的方式:
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
const state = {
animations: ['fade', 'slide', 'slideUp', 'zoom', 'flipX', 'flipY'],
view: 'slide'
}
new Vue({
render: h => h(App),
data() {
return this.state
}
}).$mount('#app')
这是我的App.vue:
<template>
<div id="app">
<component :is="state.view">
<h1>{{ state.view }}</h1>
</component>
<controls></controls>
</div>
</template>
这将是我的控件:
<template id="controls">
<ul class="controls">
<li v-for="(animation, index) in state.animations" v-bind:key="index" @click.prevent="setView(animation)" v-bind:class="{ 'active': animation === state.view }">
{{ animation }}
</li>
</ul>
</template>
<script>
export default {
template: '#controls',
methods: {
setView(animation) {
this.state.view = animation
}
}
}
</script>
..等等。不幸的是我得到:
[Vue warn]: data functions should return an object:
https://vuejs.org/v2/guide/components.html#data-Must-Be-a-Function
(found in <Root>) vue.runtime.esm.js:619
[Vue warn]: Property or method "state" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.
found in
---> <App> at src/App.vue
<Root> vue.runtime.esm.js:619
[Vue warn]: Error in render: "TypeError: this.state is undefined"
我在这里做错了什么?如何让这个东西运行?