個人的な学習のためだけの厄介なメモ
見下ろす必要はありません、時間を無駄にします
ルーティングとコンポーネント
ルートは、1対1のマッピング関係であるコンポーネントに対応します
vueおよびvue-routerライブラリを紹介します
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
vue-routerをインスタンス化します
const router = new VueRouter({
routes
})
ルートvueインスタンスにマウントします
// 从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount('#app')
ルーティングに対応するコンポーネントを作成する
const Foo = {
template: '<div>foo</div>' }
const Bar = {
template: '<div>bar</div>' }
ルーティングを構成する
const routes = [
{
path: '/foo', component: Foo },
{
path: '/bar', component: Bar }
]
ルート出口を定義する
<router-view></router-view>
<router-view class="view two" name="a"></router-view>
ルートジャンプ
//标签
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
// 字符串
router.push('home')
// 对象
router.push({
path: 'home' })
// 命名的路由
router.push({
name: 'user', params: {
userId: '123' }})
// 带查询参数,变成 /register?plan=private
router.push({
path: 'register', query: {
plan: 'private' }})
// 在浏览器记录中前进一步,等同于 history.forward()
router.go(1)
メタ情報のルーティング
$route.params
ルーティングパラメータ
//使用:
const User = {
template: '<div>User {
{ $route.params.id }}</div>'
}
const router = new VueRouter({
routes: [
{
path: '/user/:id', component: User }
]
})
//获取 $route.params
//下面几种都是获取都是通过 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 }
}
]
})
//使用:
const router = new VueRouter({
routes: [
{
path: '/promotion/from-newsletter', component: Promotion, props: {
newsletterPopup: false } }
]
})
//使用
const router = new VueRouter({
routes: [
{
path: '/search', component: SearchUser, props: (route) => ({
query: route.query.q }) }
]
})