zoukankan      html  css  js  c++  java
  • vue项目中使用ts

    npm install vue-class-component vue-property-decorator --save

    npm install ts-loader typescript tslint tslint-loader tslint-config-standard --save-dev

    • vue-class-component:扩展vue支持typescript,将原有的vue语法通过声明的方式来支持ts

    • vue-property-decorator:基于vue-class-component扩展更多装饰器

    • ts-loader:让webpack能够识别ts文件

    • tslint-loader:tslint用来约束文件编码

    • tslint-config-standard: tslint 配置 standard风格的约束

    tsconfig.json配置
    ts-loader会检索文件中的tsconfig.json.以其中的规则来解析ts文件,详细的配置可以参考https://www.tslang.cn/docs/handbook/tsconfig-json.html
    贴上测试项目tsconfig.json文件

    {
      // 编译选项
      "compilerOptions": {
        // 输出目录
        "outDir": "./output",
        // 是否包含可以用于 debug 的 sourceMap
        "sourceMap": true,
        // 以严格模式解析
        "strict": false,
        // 采用的模块系统
        "module": "esnext",
        // 如何处理模块
        "moduleResolution": "node",
        // 编译输出目标 ES 版本
        "target": "es5",
        // 允许从没有设置默认导出的模块中默认导入
        "allowSyntheticDefaultImports": true,
        // 将每个文件作为单独的模块
        "isolatedModules": false,
        // 启用装饰器
        "experimentalDecorators": true,
        // 启用设计类型元数据(用于反射)
        "emitDecoratorMetadata": true,
        // 在表达式和声明上有隐含的any类型时报错
        "noImplicitAny": false,
        // 不是函数的所有返回路径都有返回值时报错。
        "noImplicitReturns": true,
        // 从 tslib 导入外部帮助库: 比如__extends,__rest等
        "importHelpers": true,
        // 编译过程中打印文件名
        "listFiles": true,
        // 移除注释
        "removeComments": true,
        "suppressImplicitAnyIndexErrors": true,
        // 允许编译javascript文件
        "allowJs": true,
        // 解析非相对模块名的基准目录
        "baseUrl": "./",
        // 指定特殊模块的路径
        "paths": {
          "jquery": [
            "node_modules/jquery/dist/jquery"
          ]
        },
        // 编译过程中需要引入的库文件的列表
        "lib": [
          "dom",
          "es2015",
          "es2015.promise"
        ]
      }
    }

    完整代码

    <template>
      <div class="test-container">
        {{message}}
        <input type="button" value="点击触发父级方法" @click="bindSend"/>
        <input type="button" value="点击触发父级方法" @click="handleSend"/>
        <input type="button" value="点击触发父级方法" @click="bindSend2"/>
        <!-- <Hello></Hello> -->
      </div>
    </template>
    <script lang="ts">
    import { Component, Prop, Vue, Watch, Emit } from "vue-property-decorator";
    import Hello from "./HelloWorld.vue";
    // 注明此类为一个vue组件
    @Component({
      components: {
        Hello
      }
    })
    export default class Test extends Vue {
      // 原有data中的数据在这里展开编写
     public message: string = "asd";
      //原有props中的数据展开编写
      @Prop({
        type: Number,
        default: 1,
        required: false
      })
      propA?: number
      @Prop()
      propB:string
      //原有computed
      public get computedMsg(){
          return '这里是计算属性' + this.message;
      }
      public set computedMsg(message:string){
      }
      //原有的watch属性
      @Watch('propA',{
          deep:true
      })
      public test(newValue:string,oldValue:string){
          console.log('propA值改变了' + newValue);
      }
      // 以前需要给父级传值的时候直接方法中使用emit就行了,当前需要通过emit来处理
      @Emit()
      private bindSend():string{
          return this.message
      }
      @Emit()
      private bindSend1(msg:string,love:string){
          // 如果不处理可以不写下面的,会自动将参数回传
        //   msg += 'love';
        //   return msg;
      }
      //原有放在methods中的方法平铺出来
      public handleSend():void {
          this.bindSend1(this.message,'love');
      }
      // 这里的emit中的参数是表明父级通过什么接受,类似以前的$emit('父级定义的方法')
      @Emit('test')
      private bindSend2(){
          return '这个可以用test接受';
      }
    }
    </script>

    已学习,但未自己总结,详情参考

    参考

    https://www.cnblogs.com/webhmy/p/13690836.html

  • 相关阅读:
    两个时间相差多少
    JqGrid中文文档
    将A标签的href用iframe打开(JS)
    GridView 自动生成列 没有整理.
    母板页引用JS的办法
    js 判断 文本框是否被全选 ..
    jQuery 调用 Web Services 。。
    WINDOWS 7 + VS2008 +MSSQL 2005 安装成功!
    C# Serializable 的示例
    Microsoft.Crm.WebServices.Crm2007.MultipleOrganizationSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Versi
  • 原文地址:https://www.cnblogs.com/-roc/p/14451269.html
Copyright © 2011-2022 走看看