【发布时间】:2018-01-01 20:38:15
【问题描述】:
我有一个包含在这个div 中的应用程序:
<div id="app" v-bind:style='{backgroundColor: backgroundColor}'>
... the app ...
</div>
路由是按照example in the documentation完成的(这是一个webpack项目):
import Vue from 'vue/dist/vue.js'
import VueRouter from 'vue-router'
import ComponentOne from './component1.vue'
import ComponentTwo from './component2.vue'
Vue.use(VueRouter)
const routes = [{
path: '/foo',
component: ComponentOne
},
{
path: '/bar',
component: ComponentTwo
}
]
const router = new VueRouter({
routes // short for `routes: routes`
})
const app = new Vue({
router,
data: {
day: "Monday"
},
computed: {
backgroundColor: function () {
console.log(JSON.stringify(router.currentRoute))
if (router.currentRoute.path == "/foo") {
return "green"
} else {
return "blue"
}
}
}
}).$mount('#app')
我希望背景依赖于当前路线 (router.currentRoute.path)。
但是,上面的解决方案不起作用,因为 Vue 实例未检测到 router.currentRoute.path 已更改(不是响应式的)。
从 Vue 实例中访问动态路由器数据的正确方法是什么?
【问题讨论】:
标签: vue.js vue-router