【发布时间】:2021-12-03 08:32:17
【问题描述】:
正在使用动态路由。
如果vuex中没有设备数据,我想去404页面。
我应该如何实现它?
路由器/index.js
const routes = [
{
path: '/',
name: 'Main',
component: Main
},
{
path: '/:device',
name: 'Detail',
component: Detail,
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: NotFound
},
]
设备详情页如下实现时,不会移动到404页面。
const deviceName = route.params.device
const storedDeviceList = computed(() => store.state.stationName)
if (!storedDeviceList.value.includes(deviceName)) {
router.push({
name: 'NotFound'
})
}
【问题讨论】:
-
你能提供你在商店里是如何处理这个问题的吗?
-
Vuex-store 有一个包含设备列表的 stationName。
store/index.js=> state: { stationName: [ 'sample', ], } -
所以,如果stationName没有参数(router-parameter),我尝试移动到NotFound页面。
-
你能提供
deviceName和storedDeviceList的例子吗?因为我不明白,为什么这不起作用。 -
抱歉检查晚了。我在 github 上发布了一个代码示例。 (github.com/airwalk741/router-test) 状态下只有 test_device。如果您转到 test_device 以外的 URL,则会收到错误消息。 (例如localhost:8081/test123这个地方会抛出错误。)
标签: vue.js vue-router vuejs3