欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue Study [2]: Vue Router

程序员文章站 2022-06-19 12:41:49
Description The article for vue router. Original post link:https://www.cnblogs.com/markjiang7m2/p/10796020.html Source code:https://gitee.com/Sevenm2/... ......

description

the article for vue router.
original post link:
source code:https://gitee.com/sevenm2/vue.web/tree/master/vue.router

start

actually we should only remember 3 points for vue router.

  1. <router-link>. for user to click.
  2. <router-view>. to show the related content.
  3. router index.js. declare which content will be show when user click a link.

<router-link> & <router-view> is a pair of tags for vue router. they are always used together.

<router-link> will be compiled into <a> when we build the project.
there is an important property to in <router-link>. it will tell the website which path will be redirected to when user click this link.

html in app.vue

<ul>
    <li><router-link  to="/" >home</router-link></li>
    <li><router-link  to="/food" >food</router-link></li>
    <li><router-link  to="/rating">rating</router-link></li>
    <li><router-link  to="/seller">seller</router-link></li>  
</ul>

it shows that when user click the home link, the website will redirect to path /, and food link for path /food, the same as rating, seller.
actually it will be compiled as below.

<ul>
    <li><a href="#/" >home</a></li>
    <li><a href="#/food" >food</a></li>
    <li><a href="#/rating">rating</a></li>
    <li><a href="#/seller">seller</a></li>  
</ul>

router-view

when website redirect to a path, such as /food, we may want to show something different in somewhere in the page. so we should add the <router-view> tag to the place which we want to show the related content.

html in app.vue

<ul>
    <li><router-link  to="/" >home</router-link></li>
    <li><router-link  to="/food" >food</router-link></li>
    <li><router-link  to="/rating">rating</router-link></li>
    <li><router-link  to="/seller">seller</router-link></li>  
</ul>
<router-view></router-view>

so if the related content is <div>food content</div>, it will show in html as below.

<ul>
    <li><a href="#/" >home</a></li>
    <li><a href="#/food" >food</a></li>
    <li><a href="#/rating">rating</a></li>
    <li><a href="#/seller">seller</a></li> 
</ul>
<div>food content</div>

router index.js

as above, we say that <router-view> tag will be replaced by the related content, but what is the related content?
the route rule will be declared in the router index.js file. we can find this file in the path src\router\index.js as below.
Vue Study [2]: Vue Router

in the index.js file, i have declared the rule.

import vue from 'vue'
import router from 'vue-router'

vue.use(router)

export default new router({
  routes: [ 
    {
      path: '/food',
      component: { template: '<div>food content</div>' }
    },
    {
      path: '/seller',
      component: { template: '<div>seller content</div>' }
    },
    {
      path: '/rating',
      component: { template: '<div>rating content</div>' }
    }
  ]
})

as above, path /food related content is <div>food content</div>.
the result will be shown as below.

path /
we can see that the tag <router-view> is replaced as empty currently.
Vue Study [2]: Vue Router
Vue Study [2]: Vue Router

path /food
we can see that the tag <router-view> is replaced by <div>food content</div> currently.
Vue Study [2]: Vue Router
Vue Study [2]: Vue Router

in our actual development, we always have a lot of content to show in the tag <router-view>, so vue support to declare an individual component.
Vue Study [2]: Vue Router

food.vue

<template>
    <div class="content">
        <div>food content</div>
        <!-- more other contents  -->
    </div>
</template>

<script>
export default {
  name: 'food',
  data () {
    return {
      msg: 'food'
    }
  }
}
</script>

and then we can import these components in router index.js

import vue from 'vue'
import router from 'vue-router'
import food from '@/components/food'
import seller from '@/components/seller'
import rating from '@/components/rating'

vue.use(router)

export default new router({
  routes: [ 
    {
      path: '/food',
      component: food
    },
    {
      path: '/seller',
      component: seller
    },
    {
      path: '/rating',
      component: rating
    }
  ]
})

dynamic route matching

based on the above 3 route points, we can do some more extensions.
if our different paths are based on a parameter, such as an id, we can use a colon : to declare just one route in the index.js

import vue from 'vue'
import router from 'vue-router'
import food from '@/components/food'

vue.use(router)

export default new router({
  routes: [ 
    {
      path: '/:id',
      name: 'food',
      component: food
    }
  ]
})

and in the component, we can use this.$route.params to get the parameter's value.

<template>
    <div class="content">
        <div>{{$route.params.id}} content</div>
    </div>
</template>

<script>

export default {
  name: 'food',
  data () {
    return {
      msg: this.$route.params.id
    }
  }
}
</script>

Vue Study [2]: Vue Router

nested routes

sometimes, we may want to show some differences with different path based on food component. then we should use the nested routes.
firstly, i add a subfood component with a parameter.

<template>
    <div class="subcontent">
        <div>sub food content</div>
        <div>{{$route.params.id}}</div>
    </div>
</template>

<script>
export default {
  name: 'subfood',
  data () {
    return {
      msg: 'subfood'
    }
  }
}
</script>

secondly, we add links in food component, so that we can visit the subfood component.

<div>food content</div>
<div class="subnavbar">
    <ul>
    <li><router-link  to="/food/subfood1">subfood1</router-link></li>
    <li><router-link  to="/food/subfood2">subfood2</router-link></li>
    <li><router-link  to="/food/subfood3">subfood3</router-link></li> 
    </ul>
    <router-view></router-view>
</div>

and in the router index.js, we should a children property in /food section.

{
    path: '/food',
    component: food,
    children:[
    {
        path: '/food/:id',
        component: subfood
    }
    ]
}

the result is as below.
Vue Study [2]: Vue Router

Vue Study [2]: Vue Router

in this case, maybe some of you will think that as the subfood path includ /food, so its content will show in food component's <router-view>.
however, all results are because of the router index.js declaration. we can make an example as below.

delcare the subfood path in root.

{
    path: '/food',
    name: 'food',
    component: food
},
{
    path: '/seller',
    name: 'seller',
    component: seller
},
{
    path: '/rating',
    name: 'rating',
    component: rating
},
{
    path: '/food/:id',
    name: 'subfood',
    component: subfood
}

we can see that the subfood component is shown in the root's <router-view>.
the sub component will be shown in its parent component's <router-view>. if the component path is declared in root, it will be shown in the root's <router-view>
Vue Study [2]: Vue Router

named routes

we can add a name property in router index.js.

{
path: '/food',
name: 'food',
component: food,
children:[
{
    path: '/food/:id',
    name: 'subfood',
    component: subfood
}
]
}

then we can use the named routes in <router-link>. remember, we should a colon : before to property.
the below syntax are the same result.

<li><router-link :to="{name: 'food'}" >food</router-link></li>
<li><router-link  to="/food" >food</router-link></li>

if there is an parameter in the path.

<li><router-link :to="{ name: 'food', params: { id: subfood1 }}" >food</router-link></li>

named views

sometimes we need to show multiple contents in the same time. then we should add a name property for different </router-view>.
firstly, i add a subextend component.

<template>
    <div class="subcontent">
        <div>sub extend content</div>
        <div>{{$route.params.id}}</div>
    </div>
</template>

<script>
export default {
  name: 'subextend',
  data () {
    return {
      msg: 'subextend'
    }
  }
}
</script>

secondly, add a </router-view> in food component.

<div class="subnavbar">
    <ul>
    <li><router-link  to="/food/subfood1">subfood1</router-link></li>
    <li><router-link  to="/food/subfood2">subfood2</router-link></li>
    <li><router-link  to="/food/subfood3">subfood3</router-link></li> 
    </ul>
    <router-view></router-view>
    <router-view name="ex"></router-view>
</div>

the next is router index.js. as there are multiple components, we should use property components, pls. noted the s.
the default component will be shown in </router-view> without name property and the ex component will be shown in </router-view> with name property value as "ex".

{
    path: '/food',
    name: 'food',
    component: food,
    children:[
    {
        path: '/food/:id',
        name: 'subfood',
        components: {
        default: subfood,
        ex: subextend
        }
    }
    ]
}

the result is as below.
Vue Study [2]: Vue Router

redirect in js

sometimes we should do some logic judgement in js and then we could determine which path we could redirect to. we can use this.$router.push() to do this in js.

this.$router.push({ name: 'food', params: { id: "subfood1" }});
this.$router.push("/food/subfood1");

404 error page

when user want to access a url which is not nonexistent, then we should redirect to a 404 page.

first, we add a 404 component.

<template>
    <div class="content">
        <div>404 error</div>
    </div>
</template>

<script>

export default {
  name: 'error404',
  data () {
    return {
      msg: '404'
    }
  }
}
</script>

then add the route in router index.js. as we should catch all unknown url, we should declare the route in root.

export default new router({
  routes: [ 
    {
      path: '*',
      name: 'error404',
      component: error404
    },
    {
      path: '/food',
      name: 'food',
      component: food
    }
    ...
  ]
})

then we run the application and have a look.
Vue Study [2]: Vue Router

we will find that when we access the root path, we get a 404 view. it is not the result we want.
it is because we donot declare the root view in router index.js. let me to add it.
i add a home component and then declare it as the root view.

export default new router({
  routes: [ 
    {
      path: '*',
      name: 'error404',
      component: error404
    },
    {
      path: '/',
      name: 'home',
      component: home
    },
    {
      path: '/food',
      name: 'food',
      component: food
    }
    ...
  ]
})

then have a look.
Vue Study [2]: Vue Router

it shows correctly now. when we access an unknown url, the website will show the 404 view.
Vue Study [2]: Vue Router

maybe someone will say why the page will still show the menu in top. we should know that the elements in app.vue will always be shown. if we just want to show 404 view when we access an unknown page, we should move the menu elements into home component, and declare the menu components as children in home path.

home.vue

<template>
    <div class="content">
    <header>
      <nav class='navbar'> 
        <ul>
          <li><router-link  to="/" >home</router-link></li>
          <li><router-link  to="/food" >food</router-link></li>
          <li><router-link  to="/rating">rating</router-link></li>
          <li><router-link  to="/seller">seller</router-link></li>  
        </ul>
        <router-view></router-view>
      </nav>
    </header>
    </div>
</template>

<script>

export default {
  name: 'home',
  data () {
    return {
      msg: 'home'
    }
  }
}
</script>

router index.js

{
path: '/',
name: 'home',
component: home,
children:[
  {
    path: '/food',
    name: 'food',
    component: food,
    children:[
    {
      path: '/food/:id',
      name: 'subfood',
      components: {
        default: subfood,
        ex: subextend
      }
    }
  ]
  },
  ...
]
}

then the website will only show 404 view when we access an unknown url.
Vue Study [2]: Vue Router

alias

as above, when we access the root path, it shows nothing in the routerview. actually sometimes we should show something, such as dashboard in the root path. how can we do this?
there is a property alias in router. more uage description can be found in vue.

here we could only focus on the definition.
an alias of /a as /b means when the user visits /b, the url remains /b, but it will be matched as if the user is visiting /a.
we know that a path begin with / means an absolute path, without / means a relative path. so we can declare a child in home view and set alias as '' or /. it means when we access /, we are actually accessing /food.

{
path: '/',
name: 'home',
component: home,
children:[
  {
    path: '/food',
    name: 'food',
    alias: '/',
    component: food,
    children:[
    {
      path: '/food/:id',
      name: 'subfood',
      components: {
        default: subfood,
        ex: subextend
      }
    }
  ]
  },
  ...
]
}

Vue Study [2]: Vue Router

html5 history mode

maybe you have found that all our urls are including #. it's not a good link with #. is there any way to remove this char? sure. the html5 history mode will help us. more uage description can be found in vue.

we can set mode value as history in router index.js and then run the application. we can get a url without #.

export default new router({
  mode: 'history',
  routes: [ 
    ...
  ]
})

Vue Study [2]: Vue Router

build setup command line

# install dependencies
npm install

# serve with hot reload at localhost:8080
npm run dev

# build for production with minification
npm run build

# build for production and view the bundle analyzer report
npm run build --report

# run unit tests
npm run unit

# run e2e tests
npm run e2e

# run all tests
npm test