Vue 后台管理系统:如何防止重复打开子页面?
\u003cp\u003e你可以使用Vue Router来实现这种逻辑。首先,你需要在Vue项目中安装并配置Vue Router。\u003c/p\u003e\n\u003cp\u003e然后,在你的子页面组件中,你可以在路由跳转之前检查目标路由是否已经打开,如果已经打开,则取消跳转,否则进行跳转。\u003c/p\u003e\n\u003cp\u003e以下是一个示例代码:\u003c/p\u003e\n\u003cpre\u003e\u003ccode\u003e// main.js\nimport Vue from 'vue';\nimport VueRouter from 'vue-router';\nimport App from './App.vue';\nimport ChildPage1 from './components/ChildPage1.vue';\nimport ChildPage2 from './components/ChildPage2.vue';\n\nVue.use(VueRouter);\n\nconst routes = [\n { path: '/childpage1', component: ChildPage1 },\n { path: '/childpage2', component: ChildPage2 }\n];\n\nconst router = new VueRouter({\n routes\n});\n\nnew Vue({\n router,\n render: h => h(App)\n}).$mount('#app');\n\n// ChildPage1.vue\nexport default {\n methods: {\n goToChildPage2() {\n const childPage2Route = { name: 'childpage2' };\n const childPage2Exists = this.$router.getMatchedComponents(childPage2Route).length > 0;\n\n if (!childPage2Exists) {\n this.$router.push(childPage2Route);\n }\n }\n }\n}\n\n// ChildPage2.vue\nexport default {\n mounted() {\n console.log('Child Page 2 mounted');\n }\n}\n\u003c/code\u003e\u003c/pre\u003e\n\u003cp\u003e在上面的示例中,我们在ChildPage1组件中使用goToChildPage2方法来跳转到ChildPage2组件。在这个方法中,我们首先定义了要跳转到的路由对象childPage2Route,然后使用getMatchedComponents方法来检查是否已经打开了ChildPage2组件。如果没有打开,则使用$router.push方法进行跳转。\u003c/p\u003e\n\u003cp\u003e这样,每次点击打开ChildPage2组件之前都会进行检查,确保不会重复打开。\u003c/p\u003e
原文地址: https://www.cveoy.top/t/topic/pQT3 著作权归作者所有。请勿转载和采集!