1.ルーティングの役割
我々は複数のページを持っている場合は1、私たちは(ルート)のルーティングにルーティング、マッピングコンポーネント(部品)を使用して、どこにそれらをレンダリングするためにVUE-ルータに指示する必要があります。
単純なルーティング
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount('#app')
ルーティングダイナミック
マーク:「パスパラメータ」コロンを使用します。ルートの一致は、パラメータの値は、これに設定される場合。$ Route.params、各アセンブリ内で使用することができます。したがって、我々は、出力現在のユーザーのIDをユーザーテンプレートを更新することができます。
const User = {
template: '<div>User</div>'
}
const router = new VueRouter({
routes: [
// 动态路径参数 以冒号开头
{ path: '/user/:id', component: User }
]
})
ルーチンによって隠さ
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User,
children: [
{
// 当 /user/:id/profile 匹配成功,
// UserProfile 会被渲染在 User 的 <router-view> 中
path: 'profile',
component: UserProfile
},
{
// 当 /user/:id/posts 匹配成功
// UserPosts 会被渲染在 User 的 <router-view> 中
path: 'posts',
component: UserPosts
}
]
}
]
})
ルータの子ノードは、親ユーザルータビューの内部に描画されます
router.push、router.replaceとrouter.goは
あなたがrouter.pushメソッドを使用して、別のURLにナビゲートします。ブラウザの戻るボタンをユーザーがクリックすると、前のURLに戻るには、ときにこの方法は、履歴スタックに新しいレコードを追加します。
router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
router.push({ name: 'user', params: { userId: 123 }})
2.ビューに名前を付けます
レイアウトレイアウトを示します
<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>
<router-view class="view three" name="b"></router-view>
const router = new VueRouter({
routes: [
{
path: '/',
components: {
default: Foo,
a: Bar,
b: Baz
}
}
]
})
ローカルという名前のビューでは、我々は注意を払う必要があり、小道具は値以上のルートを渡すあなたがそれぞれの名前のビューを追加する必要があり、名前の景色を楽しめるルートのprops
オプションを
const User = {
props: ['id'],
template: '<div>User {{ id }}</div>'
}
const router = new VueRouter({
routes: [
{ path: '/user/:id', component: User, props: true },
// 对于包含命名视图的路由,你必须分别为每个命名视图添加 `props` 选项:
{
path: '/user/:id',
components: { default: User, sidebar: Sidebar },
props: { default: true, sidebar: false }
}
]
})
3の深さルーティングの理解
路由的配置
declare type RouteConfig = {
path: string;
component?: Component;
name?: string; // 命名路由
components?: { [name: string]: Component }; // 命名视图组件
redirect?: string | Location | Function;
props?: boolean | string | Function;
alias?: string | Array<string>;
children?: Array<RouteConfig>; // 嵌套路由
beforeEnter?: (to: Route, from: Route, next: Function) => void;
meta?: any;
// 2.6.0+
caseSensitive?: boolean; // 匹配规则是否大小写敏感?(默认值:false)
pathToRegexpOptions?: Object; // 编译正则的选项
}
実用的なアプリケーションの背後に、ドキュメントを追加----現在見ている公式文書を