# vite2-vue3-typescript
**Repository Path**: iptu/vite2-vue3-typescript
## Basic Information
- **Project Name**: vite2-vue3-typescript
- **Description**: vite2-vue3-typescript
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2021-08-01
- **Last Updated**: 2021-08-02
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## vue3.0+vite2.0+Typescript 0基础搭建新项目,后面有优化方案,现学现会,不会包退
### 全局安装一下create-vite-app
>npm -
>npm i -g create-vite-app ;
>yarn -
>yarn global add create-vite-app
### 创建项目
```ts
create-vite-app project_name
```
### 根据提示安装对应依赖
```ts
cd project_name
npm install
npm run dev
```
### 预览项目
在浏览器输入http://localhost:3000 即可预览
>vite文档给出的命令是 (要在空文件夹里面)
npm init vite-app
yarn create vite- app
等价于
全局安装 create-vite-app
然后 create-vite-app project-name
#### router
>在src目录下新建src/router/index.ts 内容如下
```ts
import { createRouter, createWebHashHistory } from 'vue-router'
// 在 Vue-router新版本中,需要使用createRouter来创建路由
export default createRouter({
// 指定路由的模式,此处使用的是hash模式
history: createWebHashHistory(),
// 路由地址
routes: [
{
path: '/',
redirect: 'about'
},
{
path: '/todo',
name: 'todo',
// 必须添加.vue后缀
component: () => import('../views/todoList.vue')
},
{
path: '/about',
name: 'about',
component: () => import('../views/about.vue')
}
]
})
```
#### stroe
> 在src目录下新建src/store/index.ts文件 内容如下
```ts
import { createStore } from 'vuex';
export default createStore({
state: {
username: '江子',
taskList: []
},
mutations: {
add(state: any, value: string) {
state.taskList.push({ name: value, isfinished: false })
},
del(state: any, index: number) {
state.taskList.splice(index, 1)
},
updates(state: any, payload: any) {
const { index, isfinished } = payload;
state.taskList[index].isfinished = isfinished
}
}
});
```
#### App.js
```ts
```
#### about.vue
>在src目录下新建src/views/about.vue文件 内容如下
```ts
{{ name }}
```
#### todoList.vue
>在src目录下新建src/views/about.vue文件 内容如下
```ts
```
#### main.ts
>把 main.js 改成 main.ts 内容如下
```ts
import { createApp } from 'vue'
import App from './App.vue'
import './index.css';
const app = createApp(App);
app.mount('#app')
// createApp(App).mount('#app')
```
---
## 优化
### about.vue 文件优化方案1
> 涉及文件:src/views/about.vue
> 把点击 操作方法 都抽离出来 放到options里面,setup里就简捷多了
```ts
{{ name }}
```
### about.vue 文件优化方案2
> 涉及文件:src/views/about.vue 和 src/utils/options.ts
> 假设 操作方法其他文件里面也要用到 可以抽离到一个文件里面去
#### options.ts
> 新建ts文件:src/utils/options.ts,把方法抽离到options文件里面去
```ts
import { ref, watch } from 'vue';
import { useStore } from 'vuex';
// 操作方法 抽离出来
function options() {
const store = useStore();
const name = ref('酱紫出品');
const inputValue = ref('');
const add = () => {
if (!inputValue.value) {
return
}
store.commit('add', inputValue.value);
inputValue.value = '';
};
const del = (i: any) => {
store.commit('del', i * 1)
console.log(i);
};
const update = (item: any, i: any) => {
store.commit('updates', { index: i * 1, isfinished: !item.isfinished })
}
return { update, add, del, inputValue, name, store }
}
export default options
```
#### about.vue
```ts
{{ name }}
```