vuecli4.0 프로젝트 typescript 도입 방법

3229 단어 vueclitypescript
기존 프로젝트는vuecli4.0 비계를 사용하여 생성되었습니다.typescript를 도입하고자 합니다.
  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 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보십시오. 앞으로 많은 응원 부탁드립니다!

좋은 웹페이지 즐겨찾기