vuejs2 - 如何在主 vue 实例中查看路由器对象
问题描述
从我的主要 vue 实例中,我试图检测路线何时更改。到目前为止,我了解到我需要深入观察对象以检测属性变化。但我的方法不起作用。我究竟做错了什么:
const app1 = new Vue({
el: '#app1',
router,
data: {
activeRoute: router.currentRoute
},
methods: {
printclass: function() {
console.log(router.currentRoute.path);
}
},
computed: {
},
watch: {
'router.currentRoute': {
handler(newVal) {
console.log('changed');
}, deep: true
}
},
created: function() {
console.log(router.currentRoute);
}
});
我知道 currentRoute 对象的 path 属性会在路由更改时更改(即呈现不同的组件)。
解决方案
观看$路线。
watch: {
'$route' (to, from) {
this.yourFunction(
},
推荐阅读
- excel - 如何按特定范围拆分长行并将其添加为新行 - Excel
- python - Matlab 等价于 Python 的“+=”运算符用于数独问题
- java - Serenity Headless Chrome 偶尔崩溃,而 Non-headless Chrome 从不崩溃
- javascript - 使用 redux 中的钩子调度操作时写入依赖数组的正确方法
- python - 绘图上的 GUI 按钮,用于选择绘图点并存储在不同的数组中
- localhost - Fiddler localhost URL(带有端口号)在带有 IIS Express 的 Visual Studio 中不起作用
- python - 根据下一行查找上一行值
- javascript - 获取html元素上的元素id名称
- python - 蟒蛇查找路径
- javascript - D3.js v5 - 从数组长度附加关于圆的线