惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
博客园 - 聂微东
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
F
Fortinet All Blogs
C
Check Point Blog
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More

zodream梦想开源/个人编程日记

文件解析笔记-zodream梦想开源/个人编程日记 密码本开发笔记之读写与保存-zodream梦想开源/个人编程日记 SkiaSharp 把 pixel byte[] 转成 SKBitmap-zodream梦想开源/个人编程日记 nas 使用 Docker 安装 gogs-zodream梦想开源/个人编程日记 复制 android 手机中的文件到电脑-zodream梦想开源/个人编程日记 周报:寻找优质的周刊-zodream梦想开源/个人编程日记 开发日志:对Markdown的代码块新增引用来源支持-zodream梦想开源/个人编程日记 周报:怎么写技术类的教程文章-zodream梦想开源/个人编程日记 css display:flex 布局尺寸超出问题-zodream梦想开源/个人编程日记 周报:SEO优化的思考-zodream梦想开源/个人编程日记 Edge 浏览器不适用 Edge Image Viewer 打开图片 -zodream梦想开源/个人编程日记 SEO 学习笔记(一) 内容来源-zodream梦想开源/个人编程日记 PHP 实现双因素身份认证(2FA)-zodream梦想开源/个人编程日记 WPF MVVM 获取List 多选数据-zodream梦想开源/个人编程日记 Burp Suite 抓包-zodream梦想开源/个人编程日记 使用 indexnow 注意事项-zodream梦想开源/个人编程日记 Godot 使用字体图标 例如: Iconfont、FontAwesome-zodream梦想开源/个人编程日记 angular 15 对指定页面进行访问限制-zodream梦想开源/个人编程日记 CSS 使用 column-count 实现瀑布流出现内容分割的解决办法-zodream梦想开源/个人编程日记 input 确认按键事件在手机端不生效-zodream梦想开源/个人编程日记 C# 使用socket 进行通讯-zodream梦想开源/个人编程日记 Maui开发中Windows应用开启管理员权限-zodream梦想开源/个人编程日记 Maui 中自定义控件-zodream梦想开源/个人编程日记 angular 14 使用 ng-template 实现tree 结构显示-zodream梦想开源/个人编程日记 c# 动态安装和卸载dll-zodream梦想开源/个人编程日记 慎用 CompositionTarget.Rendering-zodream梦想开源/个人编程日记 c# 重写 c++ 程序笔记:数据初始化-zodream梦想开源/个人编程日记 源码编译 aseprite-zodream梦想开源/个人编程日记 记录一下字符串分隔split各语言之间的不同-zodream梦想开源/个人编程日记 c# Gzip解码无头内容-zodream梦想开源/个人编程日记
升级vue3记录-zodream梦想开源/个人编程日记
zodream · 2021-08-15 · via zodream梦想开源/个人编程日记

第一步安装 vue-cli

npm install --global @vue/cli@next

1

创建项目

选择 Manually select features

勾选 Choose Vue version

Babel

Typescript

Progressive Web App (PWA) Support

Router

Vuex

Css Pre-processors 使用了Scss 必选

Linter/Formatter

Unit TestingE2E Testing 可选其中一个

选择 vue 版本 3.x

接下来输三个 y yes

然后选择 Sass/SCSS

ESLint 规则选择: 选了第一个,碰到很多问题,比如 在 template 中 {{ 1 < 2 }} 小于号居然被提示不符合vue 规则

选择 Lint on save 代码规范检查的时机

保存设置 In dedicated config files

然后 是否保存这个配置方便以后创建其他项目 N

安装一些其他依赖

npm install mitt axios vue-class-component@next vue-property-decorator@rc vuex-class vuex-class-modules

1

axios Api数据请求

mitt 全局事件管理,vue3 取消了 $once 等全局事件

vue-class-component@next vue3 Typescript 项目默认使用vue-class-component, 这里是为了安装最新

vue-property-decorator@rc @Prop() 的使用

vuex-class vuex-class-modules 方便vuex 使用

修改代码

复制一些文件和资源

修改项

  1. 修改 @Component@Options

  2. axios 注册全局

export default {
    install(app: any) {
        app.config.globalProperties.$post = post
        app.config.globalProperties.$fetch = fetch
        app.config.globalProperties.$patch = patch
        app.config.globalProperties.$put = put
    },
}

12345678

  1. VueRouter 改为 import { Router } from 'vue-router';
  2. 路由直接放 const routes: Array<RouteRecordRaw> = [];
  3. Vuex 使用
    
    import {
     SET_USER, TOKEN_KEY, SET_TOKEN,
    } from '../types';
    import { IUser, ILogin } from '@/api/model';
    import { getSessionStorage, setSessionStorage, removeSessionStorage } from '@/utils';
    import { getProfile, login, logout } from '@/api/user';
    import { Action, Module, Mutation, VuexModule } from 'vuex-class-modules';

    12345678

@Module({ generateMutationSetters: true }) export class AuthModule extends VuexModule { token: string | null = null; user: IUser | null = null;

get isGuest() {
    if (this.user) {
        return false;
    }
    const token = getSessionStorage<string>(TOKEN_KEY);
    return !token;
}

@Mutation
[SET_USER](user: IUser|null) {
    this.user = user;
}


@Action
logoutUser() {
    return new Promise<void>((resolve, reject) => {
        const token = getSessionStorage<string>(TOKEN_KEY);
        if (!token) {
            resolve();
            return;
        }
        logout().then(() => {
            this[SET_TOKEN](null);
            this[SET_USER](null);
            resolve();
        }).catch(reject);
    });
}

}

import { createStore } from 'vuex'; import { AuthModule } from './modules/auth';

const store = createStore({});

export const authModule = new AuthModule({store, name: 'auth'});

export default store;

具体参考 [Vue.js with Typescript and Decorators](https://davidjamesherzog.github.io/2020/12/30/vue-typescript-decorators/)

6. 修改main.ts
```ts
import { createApp } from 'vue';
import App from './App.vue';
import './registerServiceWorker';
import router from './router';
import store from './store';
import emitter from './event';

import './assets/iconfont/iconfont.css';

import http from './utils/http';

createApp(App, {
    onscroll(e: Event) {
        emitter.emit('scroll', e); // 传递滚动事件
    }
}).use(http).use(store).use(router).mount('#app');

1234567891011121314151617181920

  1. $children 被删除了,需要改动使用 setup() 获取子元素
  2. filter 已经删除了,所以只能通过方法调用

转载请保留原文链接: https://zodream.cn/blog/id/216.html