Vue Cli3 TypeScript 搭建工程
Vue Cli3出来也一段时间了,我想尝试下Vue结合TypeScript搭建个工程,感受下Vue下用TS...网上有一篇讲的非常详细的教程 vue-cli3.0 搭建项目模版教程(ts+vuex+axios) 作者:陈小生_1017
我看完教程后(好长的一篇博文,不得不服作者的用心,赞!),我去博主留的git地址 https://github.com/chenfangsheng/vue-cli3-tpl.git 克隆一份下来,安装完依赖后,发现好多错误...汗...我在原博客评论区和git issue区均为发现问题的解决办法,我尝试着一番google后,项目能跑起来了,顺便研究了下vuex-class的用法,下面会贴出具体的用法。出现的错误有:
1.引入scss的路径不对,按照下边改为相对路径就可以了
// vue-cli3-tpl/src/components/test/test.vue ...
<style lang="scss">
/*@import "@/assets/scss/variables";*/
@import "../../assets/scss/variables"; .test-wrap {
width: 100%;
color: $background-color;
}
</style>
// vue-cli3-tpl/src/assets/scss/variables.scss $background-color : #4c94f1;
为了方便,我们引用scss全局变量,在每一个style标签引入这个公共变量文件太麻烦了,官方有全局引入的方法:
// vue.config.js
css: {
modules: false, // 启用 CSS modules
extract: true, // 是否使用css分离插件
sourceMap: true, // 开启 CSS source maps?
loaderOptions: {
// 给 sass-loader 传递选项
sass: {
// @/ 是 src/ 的别名
// 所以这里假设你有 `src/assets/scss/variables.scss` 这个文件
data: `@import "@/assets/scss/variables.scss";`
}
} // css预设器配置项
},
2.TS报错: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'LoginState'. No index signature with a parameter of type 'string' was found on type 'LoginState'
// vue-cli3-tpl/src/store/module/login.ts
// 更改state
const mutations: MutationTree<LoginState> = {
// 更新state都用该方法
UPDATE_STATE(state: LoginState, data: LoginState) {
for (const key in data) {
if (!data.hasOwnProperty(key)) { return }
state[key] = data[key] // TS7053错误
}
}
}
我们在js中访问对象时,[]中的name的类型必须是string,但是在ts中name的隐式类型为any,所以ts发现没有类型是string的的索引标记。。。解决办法:指定对象中key的类型
// vue-cli3-tpl/src/types/views/login.interface.ts
// VUEX login.State 参数类型
export interface LoginState {
[key: string]: any
}
最后贴下我 vuex-class 的使用:
// vue-cli3-tpl/src/store/index.ts
import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex) // modules
import login from './module/login'
import index from './module/index' export default new Vuex.Store({
state: {
//
},
mutations: {
//
},
actions: {
//
},
modules: {
login,
index
}
})
// vue-cli3-tpl/src/store/module/login.ts
import { LoginState } from '@/types/views/login.interface'
import { GetterTree, MutationTree, ActionTree } from 'vuex'
import * as LoginApi from '@/api/login' const state: LoginState = {
author: 'edison',
age: 18,
obj: null
} // 强制使用getter获取state
const getters: GetterTree<LoginState, any> = {
getAuthor: (state: LoginState) => state.author
} // 更改state
const mutations: MutationTree<LoginState> = {
// 更新state都用该方法
UPDATE_STATE(state: LoginState, data: LoginState) {
for (const key in data) {
if (!data.hasOwnProperty(key)) { return }
state[key] = data[key]
}
}
} const actions: ActionTree<LoginState, any> = {
UPDATE_STATE_ASYN({ commit, state: LoginState }, data: LoginState) {
commit('UPDATE_STATE', data)
},
async GET_DATA_ASYN({ commit, state: LoginState }) {
const result = await LoginApi.getData()
commit('UPDATE_STATE', {'obj': result.data})
return result.data
}
} export default {
namespaced: true,
state,
getters,
mutations,
actions
}
// vue-cli3-tpl/src/views/login/login.ts
import { Component, Vue } from "vue-property-decorator"
import {Getter, Action, namespace, State} from "vuex-class"
import { LoginData } from '@/types/views/login.interface'
import { Test } from "@/components" // 组件 const LoginModule = namespace('login') @Component({
components: {
Test
}
})
export default class About extends Vue {
// Getter
// @Getter login.author // Action
// 和 @State("author") author 相同
@LoginModule.State('author') author!: string
// 直接映射 @State age
@LoginModule.State age!: number
@LoginModule.State obj!: any /**
* state映射到组件时,是放在computed下的,因此本身为计算属性
* */
// 只从state获取
@LoginModule.State(state => state.author) author1!: string
// 从 state 和 getters 上获取
@LoginModule.State((state, getters) => state.author + getters.getAuthor) author2!: string // 内部使用namespace 如果不想在外部使用namespace,可以使用参数传递namespace
@State('author', {namespace: 'login'}) author3!: string // Getter
@Getter("login/getAuthor") getAuthor!:string;
@Getter("getAuthor",{namespace:"login"}) getAuthor1!:string;
@LoginModule.Getter('getAuthor') getAuthor2!:string; // Action
@LoginModule.Action GET_DATA_ASYN!: Function
@LoginModule.Action UPDATE_STATE_ASYN!: Function // data
data: LoginData = {
pageName: 'login'
} created() {
this.GET_DATA_ASYN()
.then((data: any) => {
console.log('data ', data)
debugger
})
console.log('state用法')
console.log('this.author ', this.author)
console.log('this.author1 ', this.author1)
console.log('this.author2 ', this.author2)
console.log('this.author3 ', this.author3)
console.log('getter用法')
console.log('this.getAuthor ', this.getAuthor)
console.log('this.getAuthor1 ', this.getAuthor1)
console.log('this.getAuthor2 ', this.getAuthor2)
console.log('this.age ', this.age)
console.log('action用法')
this.UPDATE_STATE_ASYN(
{
author: 'edison modified'
}
)
console.log('modified author ', this.author)
} activated() {
//
} mounted() {
//
} // 初始化函数
init() {
//
} }
参考博文:如何使用 vue + typescript 编写页面 ( vuex装饰器部分 )
Vue Cli3 TypeScript 搭建工程的更多相关文章
- 使用VUE CLI3.0搭建项目vue2+scss+element简易版
1.安装Vue CLI 3 //三选一即可cnpm install -g @vue/cli npm install -g @vue/cli yarn global add @vue/cli 注意: 1 ...
- Vue CLI3和Vue CLI2环境搭建
关于 Vue CLI 旧版本的安装以及创建项目 1.搭建 vue 的开发环境 ,安装 vue 的脚手架工具 官方命令行工具 npm install --global vue-cli / cnpm in ...
- vue cli3 项目配置
[转]https://juejin.im/post/5c63afd56fb9a049b41cf5f4 基于vue-cli3.0快速构建vue项目 本章详细介绍使用vue-cli3.0来搭建项目. 本章 ...
- VUE CLI3.X 创建项目
Node.js环境搭建 Node.js基于V8引擎,可以让js代码脱离浏览器运行 Vue CLI3.0 需要Node.js 8.9或者更高版本. 用nvm或者nvm-windows在同一台电脑中管理多 ...
- 安装VUE Cli3 框架方法
下面为大家介绍一下怎样安装 VUE Cli3的步骤 官网地址 https://cli.vuejs.org/zh/guide/installation.html 一.首先要检查一下是否安装node环 ...
- vue项目ide(vue项目环境搭建)
一.先介绍一下我接下来要做的项目 项目:ide可视化工具 技术应用: Vue2.0(js框架):https://cn.vuejs.org/ ElementUi(饿了吗ui框架基于vue的):http: ...
- vue开发环境搭建Mac版
一.前言 要做一个移动端app,面对webapp最流行的三个技术React,angular,vue,三选一,如何选,可参考blog移动app技术选型,react,angular, vue, 下面是对 ...
- vue cli3超详细创建多页面配置
1.首先按照vue cli3 给的入门文档下载个vue cli3 如果之前下载了vue cli2的要先卸载之前的 2.检查安装是否成功 3.ok,现在环境搭建好了,新建项目 vue create he ...
- Vue CLI3 关闭热替换后出现的warning
用vue cli3做项目的时候如果开启了typescript的严格模式,在dev server热替换的时候往往就会打出一大堆warning,严重的影响了编译效率.官方并没有提供关闭warning的ap ...
随机推荐
- Python3 实现FTP功能
目录结构: FTP_project/ ├── FTP_client │ ├── ftp_client.py │ └── __init__.py └── FTP_server ├── bin │ ...
- sh_07_买苹果增强版
sh_07_买苹果增强版 # 1. 输入苹果的单价 price_str = input("苹果的单价:") # 2. 输入苹果的重量 weight_str = input(&quo ...
- CF643E Bear and Destroying Subtrees
题解 我们可以先写出\(dp\)式来. 设\(dp[u][i]\)表示以\(u\)为根的子树深度不超过\(i-1\)的概率 \(dp[u][i]=\prod (dp[v][i-1]+1)*\frac{ ...
- webstorm tools window
webstorm左侧的文件列表不见了, 通过菜单,view-->tools window-->project window就可以找到
- PowerDesigner相关总结
1.PowerDesigner 工具生成数据库Report指导 摘自:https://www.cnblogs.com/zycblog/archive/2010/05/11/1732918.html 1 ...
- P1080国王游戏
传送 最大值最小什么的一看就是二分了qwq 然鹅并不知道怎么检查,所以我们换个思路 我们要求出最小的最大值,这肯定和大臣的排列有关,会不会有什么规律? 先看看只有两个大臣的情况 排列:1 2,ans1 ...
- 查看在linux中下载的图片
1.安装 yum install lrzsz -y 2.找到文件所在的位置选中之后 3.点击那个蓝色的框框里面有一个 用ZMODEM下载 4.选择要保存的位置就可以查看了
- Sails.js中文文档,Sails中文文档
Sails.js中文文档 http://sailsdoc.swift.ren/ Sails.js是一个Web框架,可以于轻松构建自定义,企业级Node.js Apps.它在设计上类似于像Ruby ...
- Python字典的json格式化处理(换行与不换行)
Prefer = {"jim": {"War": 1.9, "the big bang": 1.0, "The lord of w ...
- python字典小知识
字典的小知识dic = {"name": "tom", "age": 23, "price": 110}# 01:提取键 ...