Vue3: routing case

1. Install and configure vue-router 4.x

1. Run the following command to install vue-router:

```clike
npm install vue-router@next -S 
```

2. Create a new router.js routing module in the src directory:

```clike
// 1. 按需导入对应的函数
import { createRouter, createWebHashHistory } from 'vue-router'
// 2. 创建路由对象
const router = createRouter({
history: createWebHashHistory(),
routes: [],
})
// 3. 向外共享路由实例对象
export default router
```

3. Import and mount the routing object in the main.js entry file:

```clike
// 1. 导入路由模块
import router from './router'
const app = createApp(App)
// 2. 挂载路由对象
app.use(router)
app.mount('#app')
```

2. Show Login.vue login component

1. Import the Login.vue component in the router.js module:

import Login from './components/MyLogin.vue'

2. Declare routing rules as follows:

routes: [
// 路由重定向
{
   
    
     path: '/', redirect: '/login' },
{
   
    
     path: '/login', component: Login },
]

3. Declare the route placeholder in the App.vue component:

<template>
<!-- 路由的占位符 -->
<router-view></router-view>
</template>
<script>
export default {
   
    
    
name: 'MyApp',
}
</script>
<style lang="less" scoped>
</style>

3. Simulate and realize the login function

1. Declare the following data in the MyLogin.vue component:

data() {
   
    
    
return {
   
    
    
username: '',
password: '',
}
},

2. Perform v-model two-way data binding for the text boxes of username and password:

<!-- 登录名称 -->
<div class="form-group form-inline">
<label for="username">登录名称</label>
<input type="text" class="form-control ml-2" id="username"
placeholder="请输入登录名称" autocomplete="off" v-model="username">
</div>
<!-- 登录密码 -->
<div class="form-group form-inline">
<label for="password">登录密码</label>
<input type="password" class="form-control ml-2" id="password"
placeholder="请输入登录密码" v-model="password">
</div>

3. Bind the click event handler function to the login button:

<button type="button" class="btn btn-primary"
@click="onLoginClick">登录</button>

4. Declare the onLoginClick event handler in methods as follows

methods:

Guess you like

Origin blog.csdn.net/u013308709/article/details/128697856