vuecli4.0 프로젝트 typescript 도입 방법
3229 단어 vueclitypescript
1.설치 명령 실행
npm install --save-dev typescript
npm install --save-dev @vue/cli-plugin-typescript
2.루트 디렉터리에 새 tsconfig를 만듭니다.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"allowJs": false,
"noEmit": true,
"types": ["webpack-env"],
"paths": {
"@/*": ["src/*"]
},
"lib": ["esnext", "dom", "dom.iterable", "scripthost"]
},
"exclude": ["node_modules"]
}
3.shims-vue가 추가되었습니다.d.ts루트 디렉터리에 새로 만든shims-vue.d.ts, ts가 식별할 수 있도록 *.vue 파일, 파일 내용은 다음과 같습니다.
declare module '*.vue' {
import Vue from 'vue';
export default Vue;
}
4.포털 파일 접미사 수정
src/main.js => src/main.ts
5.개조vue 파일
src/main.js => src/main.ts
lang=ts를 더하면 웹팩이 이 코드를 typescript로 식별할 수 있습니다6.데코더 플러그인 사용
vue-class-component: Vue 구성 요소를 강화하고 TypeScript 장식기를 사용하여 Vue 구성 요소를 강화하여 구성 요소를 더욱 납작하게 합니다
vue-property-decorator: vue-class-component에서 Vue 특성을 결합한 장식을 더욱 강화한다
Demo:
import { Vue, Component ,Watch} from 'vue-property-decorator';
@Component({
components: { Loading }
})
export default class App extends Vue{
old_back:object=null,
transitionName:string = "slide-right";
get ...mapState("base", ["loadingStatus"]);
@Watch('$route')
onChangeValue(to:object, from:object){
// console.log('$route', to, from)
const noBack = to.meta.noBack; //
// ,
if (to.meta.last) {
this.transitionName = "slide-left";
} else if (from.meta.last) {
this.transitionName = "slide-right";
} else if (from.meta.leaf) {
// ,
this.transitionName = "slide-right";
} else {
if (noBack) {
//
this.transitionName = "slide-right";
} else {
this.transitionName = "slide-left";
}
}
}
@Watch('loadingStatus')
onChangeValue(newVal: string){
if (newVal) {
setTimeout(_ => {
this.setLoading(false);
}, 1500);
}
}
//
showAlert(msg:string) {
plus.nativeUI.alert(
msg,
function() {
// console.log("User pressed!");
},
" ",
" "
);
}
}
이 vuecli4.0 프로젝트의 typescript 도입에 관한 글은 여기까지 소개되었습니다. 더 많은 vuecli4.0 도입 typescript 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보십시오. 앞으로 많은 응원 부탁드립니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Fastapi websocket 및 vue 3(Composition API)1부: FastAPI virtualenv 만들기(선택 사항) FastAPI 및 필요한 모든 것을 다음과 같이 설치하십시오. 생성main.py 파일 및 실행 - 브라우저에서 이 링크 열기http://127.0.0.1:...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.