vue.js - 如何在 Vue.Js 中使用子路径
问题描述
这段代码
index.js
{
path: '/test',
name: 'test',
component: Test,
children: [{
path: 'p1',
name: 'p1',
component: P1
}]
}
在组件测试中
test.vue
<template>
<div>
<h1>Test</h1>
<button v-on:click="navigateTo('/test/p1')">p1</button>
</div>
</template>
<script>
export default {
methods: {
navigateTo(route) {
this.$router.push(route)
}
}
}
</script>
在组件 P1 中
<template>
<div>
<h1>P1</h1>
</div>
</template>
当我单击按钮 p1 时,我在浏览器中有路径,但没有加载组件 p1。为什么?? 我是 vue.js 的新手
解决方案
我认为您错过了<router-view></router-view>
父模板中的 (in test.vue
)。路由器视图是渲染子组件的地方。IE
<template>
<div>
<h1>Test</h1>
<button v-on:click="navigateTo('/test/p1')">p1</button>
<router-view></router-view>
</div>
</template>
推荐阅读
- javascript - 将 js 文件编译到另一个目录没有按预期工作
- python - 黑色格式化程序 - Python
- java - 如何开始从提供的 html 文件中提取信息
- javascript - React Native Redux 调度不起作用但没有错误
- javascript - React Router 仅在刷新页面后才起作用
- angular - 无法理解,为什么 SVG 的某些部分在从角度组件生成时最初没有绘制?
- typescript - TypeScript 中的泛型比较
- javascript - JSON.stringify 在模板文字中的 map 方法中不起作用
- c++ - 重温:C++中静态数组和动态数组的区别?
- java - 无法反序列化 HashSet 的实例