diff --git a/demo/router-components/App.vue b/demo/router-components/App.vue new file mode 100644 index 0000000..7cdf6f4 --- /dev/null +++ b/demo/router-components/App.vue @@ -0,0 +1,12 @@ + + diff --git a/demo/router-components/components/Header1.vue b/demo/router-components/components/Header1.vue new file mode 100644 index 0000000..ba80fcf --- /dev/null +++ b/demo/router-components/components/Header1.vue @@ -0,0 +1,13 @@ + + + + + diff --git a/demo/router-components/components/Header2.vue b/demo/router-components/components/Header2.vue new file mode 100644 index 0000000..caeedb0 --- /dev/null +++ b/demo/router-components/components/Header2.vue @@ -0,0 +1,13 @@ + + + + + diff --git a/demo/router-components/main.js b/demo/router-components/main.js new file mode 100644 index 0000000..ff25769 --- /dev/null +++ b/demo/router-components/main.js @@ -0,0 +1,5 @@ +import {createApp} from "vue"; +import App from "./App.vue" +import router from './router' + +createApp(App).use(router).mount('#app') diff --git a/demo/router-components/router/index.js b/demo/router-components/router/index.js new file mode 100644 index 0000000..0d1e9ca --- /dev/null +++ b/demo/router-components/router/index.js @@ -0,0 +1,39 @@ +// 引入必要的router组件 +import {createRouter, createWebHistory} from 'vue-router' +// 引入视图 + +const Home = ()=>import('../views/HomeView.vue') +const About = () => import('../views/AboutView.vue') +const Header1 = () => import('../components/Header1.vue') +const Header2 = () => import('../components/Header2.vue') + + +// 创建路由,如果同一个路由中包含多个组件,则将component改名为components(加s),已对像的形式加添加多个组件,对象中的key是对象的名称, 可以在视图中使用此名称指定组件 +const routes = [ + { + path: '/', + name: 'home', + components: { + header: Header1, + default: Home, + } + }, + { + path: '/about', + name: 'about', + components: { + header: Header2, + default: About, + } + }, +] + +// 创建路由器,使用createRouter方法,传入history(路由类型)和 创建好的路由routes +// 路由类型分为hash型和html5型,分别为createWebHashHistory和createWebHistory +const router = createRouter({ + history: createWebHistory(), + routes +}) + +// 导出路由 +export default router diff --git a/demo/router-components/views/AboutView.vue b/demo/router-components/views/AboutView.vue new file mode 100644 index 0000000..89057b2 --- /dev/null +++ b/demo/router-components/views/AboutView.vue @@ -0,0 +1,12 @@ + + + diff --git a/demo/router-components/views/HomeView.vue b/demo/router-components/views/HomeView.vue new file mode 100644 index 0000000..8af0b4b --- /dev/null +++ b/demo/router-components/views/HomeView.vue @@ -0,0 +1,3 @@ + diff --git a/src/App.vue b/src/App.vue index 0af406b..7cdf6f4 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,26 +2,11 @@ + diff --git a/src/components/Header1.vue b/src/components/Header1.vue new file mode 100644 index 0000000..ba80fcf --- /dev/null +++ b/src/components/Header1.vue @@ -0,0 +1,13 @@ + + + + + diff --git a/src/components/Header2.vue b/src/components/Header2.vue new file mode 100644 index 0000000..caeedb0 --- /dev/null +++ b/src/components/Header2.vue @@ -0,0 +1,13 @@ + + + + + diff --git a/src/router/index.js b/src/router/index.js index d26718e..0d1e9ca 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,39 +1,31 @@ // 引入必要的router组件 -import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router' +import {createRouter, createWebHistory} from 'vue-router' // 引入视图 -import HomeView from '../views/HomeView.vue' -const Header = () => import('../views/HeaderView.vue') -const Test = () => import('../views/TestView.vue') +const Home = ()=>import('../views/HomeView.vue') +const About = () => import('../views/AboutView.vue') +const Header1 = () => import('../components/Header1.vue') +const Header2 = () => import('../components/Header2.vue') -// 创建路由,包含path,name,component属性, component属性可以使用箭头函数直接引入视图 + +// 创建路由,如果同一个路由中包含多个组件,则将component改名为components(加s),已对像的形式加添加多个组件,对象中的key是对象的名称, 可以在视图中使用此名称指定组件 const routes = [ { path: '/', name: 'home', - component: HomeView + components: { + header: Header1, + default: Home, + } }, { path: '/about', name: 'about', - component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue') - }, - // 配合params参数的路由path改造 - { - path: '/about/:id/:name', - name: 'about-params', - component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue') + components: { + header: Header2, + default: About, + } }, - { - path: '/header', - name: 'header', - component: Header, - }, - { - path: '/test', - name: 'test', - component: Test, - } ] // 创建路由器,使用createRouter方法,传入history(路由类型)和 创建好的路由routes diff --git a/src/views/HeaderView.vue b/src/views/HeaderView.vue deleted file mode 100644 index a4e983c..0000000 --- a/src/views/HeaderView.vue +++ /dev/null @@ -1,18 +0,0 @@ - - - diff --git a/src/views/TestView.vue b/src/views/TestView.vue deleted file mode 100644 index f31e9a5..0000000 --- a/src/views/TestView.vue +++ /dev/null @@ -1,43 +0,0 @@ - - - - - -