关于vue.js:基于Vue3-Vite-TS二次封装elementplus业务组件

38次阅读

共计 1777 个字符,预计需要花费 5 分钟才能阅读完成。

威❤ itspcool 一起交流学习

装置 vue-router,vuex
npm i vue-router@next vuex@next
vue-router
在 src 目录下新建 router/index.ts

import {createRouter, createWebHistory, RouteRecordRaw} from ‘vue-router’
import HelloWorld from ‘../components/HelloWorld.vue’
const routes: Array<RouteRecordRaw> = [
{

path: '',
redirect: (_) => {return { path: '/home'}
},

},
{

path: '/home',
name: 'HelloWorld',
component: HelloWorld,

},
{

path: '/about',
name: 'About',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () =>
  import(/* webpackChunkName: "About" */ '../components/About.vue'),

},
{

path: '/:currentPath(.*)*', // 路由未匹配到,进入这个
redirect: (_) => {return { path: '/404'}
},

},
]
const router = createRouter({
history: createWebHistory(”),
routes,
scrollBehavior(to, from, savedPosition) {

return {
  el: '#app',
  top: 0,
  behavior: 'smooth',
}

},
})
export default router
在 src 目录下再新建一个 components/About.vue 文件,内容如下:

<template>
<h1>{{msg}}</h1>
</template>
<script lang=”ts”>
import {defineComponent} from ‘vue’
export default defineComponent({
name: ‘About’,
data() {

return {msg: 'About Page',}

},
setup() {},
})
</script>
vuex
在 src 目录下创立 store/index.ts

import {InjectionKey} from ‘vue’
import {createStore, Store} from ‘vuex’

export interface State {
count: number
}

export const key: InjectionKey<Store<State>> = Symbol()

export const store = createStore<State>({
state() {

return {count: 0}

},
mutations: {

increment(state) {state.count++}

}
})
最初批改下 App.vue

<template>
<img alt=”Vue logo” src=”./assets/logo.png” />
<router-view />
</template>
<script lang=”ts”>
import {defineComponent} from ‘vue’
import HelloWorld from ‘./components/HelloWorld.vue’
export default defineComponent({
name: ‘App’,
components: {

HelloWorld

}
})
</script>
<style>

app {

font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

正文完
 0