安装

安装 @vue/cli-plugin-router 插件

使用

注册路由

router 文件夹中的 index.js 中将路由添加到 routes 数组中例如:

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },
  {
    path: '/404',
    name: '404',
    component: NotFoundView
  },
]

注意:有使用到的组件要import进index.js中

重定向示例:

{
path: '/:catchAll(.*)',
redirect: '/404/',
}

:catchAll() 是关键字,括号里面接收正则表达式,这里的 . 表示任意字符, 表示任意多个 .* ,
redirect 表示重定向的链接。

使用参数的示例:

{
    path: '/userprofile/:userId/',
    name: 'userprofile',
    component: UserProflieView
}

这里 userId 是一个变量,在要当作变量的路由前面加上 : 即可。

在组件中使用路由参数,需要使用 useRoute 组件
示例:

import { useRoute } from 'vue-router';
export default {
name: 'UserProfileView',
components: {
    ContentBase,
},
setup() {
    const route = useRoute();
    console.log(route.params.userId);
}

对应的跳转:

<router-link class="nav-link" :to="{name: 'userprofile', params: {userId : 2,}}">用户动态</router-link>

需要在params对象中添加对应的参数。

跳转

<router-link class="navbar-brand" :to="{name: 'home', params: {}}">Myspace</router-link>

router-link 是vue中的一个组件,使用 : + 名称绑定属性,这里绑定一个to属性表示要跳转的组件,里面是一个对象,对象一个key是name,表示组件的名称

在函数中使用跳转示例:

import router from '@/router/index'
success() {
  router.push({name: 'userlist'});
},

使用 routerpush api 里面传的参数是一个对象,和 router-link:to 是一样的。

组件中路由的使用

使用 <router-view /> 表示这个标签通过链接的不同渲染不同的组件。
一般放在 App.vue 中, 例如:

<template>
  <NavBar />
  <router-view />
</template>

这里 NavBar 一直保持不变, router-view 根据链接的不同渲染不同页面。
一般链接中可能会带有参数,例如 /userprofile/65/userprofile/32 ,如果想要严格判断完整的路径可以给 router-view 加一个属性 :key="$route.fullPath"
例如:

<template>
  <NavBar />
  <router-view :key="$route.fullPath"/>
</template>

如果不加的话,/userprofile/65/userprofile/32 这两个页面可能显示一样的内容。(比如先访问 /userprofile/65 后访问 /userprofile/32 ,可能页面还是 /userprofile/65 的内容而不是 /userprofile/32 的内容,原因就是只匹配 userprofile 这个路径,而没有匹配后面的参数 )。

虽然退了,但是想要收鸡的心情却一直不消减,刚一直在刷论坛看到中了claw的另外一款vps,然后果断私聊,然后狠狠剁手😭。
之后打算慢慢把博客搬迁到新的vps上,然后现在博客在的这个vps就放生掉吧,(不然真的养不起那么多vps了😭

这几天claw有活动,有一个首年7刀的vps
刚好没有新加坡的节点,就上头下单了。
续费有点贵要到36刀,有点可惜,只能当年抛来用了,或者明年再看下?

每一个组件由三个部分组成分别是 template script style
其中style部分可以在头标签加上一个 scoped 字段,之后者部分的style就不会影响到其他组件
script部分需要export一个对象,这个对象有两个key,一个是name表示当前组件的名字,另外一个是components表示当前组件用到的组件。有用到的组件也同时需要在script标签中用import关键字来引入。引入之后的组件可以在template中通过标签的形式来使用。

例如:
App.vue

<template>
  <NavBar />
  <router-view/>
</template>

<script>
import NavBar from './components/NavBar.vue';
export default {
  name: "App",
  components: {
    NavBar
  }
}
</script>

<style scoped>
</style>

假如在一个组件内使用其他组件,且那个被使用的组件的标签中有其他的内容,在那个被使用的组件内可以使用 <slot></slot> 标签将传入的内容全部渲染出来
比如:
ContentBase.vue

<template>
    <div class="home">
      <div class="container">
        <div class="card">
          <div class="card-body">
            <slot></slot>
          </div>
        </div>
      </div>
    </div>   
</template>

<script>
export default {
    name: "ContentBase",
}
</script>

<style scoped>
.container {
    margin-top: 20px;
}
</style>

安装

在vue ui 中项目依赖处搜索 bootstrap 然后安装
同时也需要安装 @popperjs/core

使用

在 main.js 中加入

import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/js/bootstrap';