Logo
Section 1 / 6

路由

学习如何使用 Vue Router 创建和设置路由参数。

导入

路由

学习如何使用 Vue Router 创建和设置路由参数。

...
1import HelloWorld from './components/HelloWorld'; 2const routes = [ 3 { 4 path: '/hello-world', 5 component: HelloWorld 6 } 7];
1 2{ 3 name: 'blog', 4 path: '/blog/:slug', 5 component: BlogPost 6}
1export default { 2 name: 'BlogPost', 3 data: () => ({ 4 title: '', 5 content: '' 6 }), 7 created() { 8 const { slug } = this.$route.params; 9 fetch(`/api/posts/${slug}`).then(res => res.json()) 10 .then(post => { 11 this.title = post.title; 12 this.content = post.content; 13 }); 14 } 15}

一个完整的 Vue 路由代码示例

组合式 API

导航