Vue 获取URL中的参数

实现效果如下:

获取URL中的参数,并显示在页面上

Vue 获取URL中的参数_第1张图片

流程:

1.在index.js中编辑代码如下:

import {createRouter,createWebHashHistory} from 'vue-router'
import News from '../components/News.vue'
import Home from "../components/Home.vue"


// 1. Define route components.
// These can be imported from other files


// 2. Define some routes
// Each route should map to a component.
// We'll talk about nested routes later.
const routes = [
    {
        path: '/',
        component: Home
    },
    {
        path:"/news/:id*",
        component:News
    },
 
]

// 3. Create the router instance and pass the `routes` option
// You can pass in additional options here, but let's
// keep it simple for now.
const router = createRouter({
    // 4. Provide the history implementation to use. We are using the hash history for simplicity here.
    history: createWebHashHistory(),
    routes, // short for `routes: routes`
})

export default router

2.新建News.vue

Vue 获取URL中的参数_第2张图片

News.vue:





你可能感兴趣的:(Vue基础,前端)