最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于TypeScript的踩坑記錄

 更新時間:2022年09月23日 10:20:15   作者:lihefei_coder  
這篇文章主要介紹了關(guān)于TypeScript的踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

用字符串做下標報錯

代碼示例

const person = {
    name: '張三',
    age: 10
};
function getValue(arg: string) {
    return person[arg];
}

錯誤信息

Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type ‘{ name: string; age: number; }’.
No index signature with a parameter of type ‘string’ was found on type ‘{ name: string; age: number; }’.ts(7053)

解決方法1

在tsconfig.json中配置suppressImplicitAnyIndexErrors: true

{
    "compilerOptions": {
        "suppressImplicitAnyIndexErrors": true,
        ...
    },
    ...
}

解決方法2

給person定義接口

const person = {
    name: '張三',
    age: 10
};
function getValue(arg: string) {
	interface IPerson {
		[key: string]: any
	}
    return (<IPerson>person)[arg];
}

函數(shù)內(nèi)使用this報錯

代碼示例

function test() {
    this.title = 'hello'; 
}

錯誤信息

‘this’ implicitly has type ‘any’ because it does not have a type annotation.ts(2683)

解決方法

在tsconfig.json中配置noImplicitThis: true

{
    "compilerOptions": {
        "noImplicitThis": true,
        ...
    },
    ...
}

找不到模塊XXX

代碼示例

import CryptoJS from 'crypto-js';

錯誤信息

Cannot find module ‘crypto-js’.ts(2307)

解決方法

安裝對應的聲明文件

cnpm install --save-dev @types/crypto-js

模塊聲明文件搜索: https://microsoft.github.io/TypeSearch/

如果安裝不了或搜不到聲明文件,請看下面這種方法

引入模塊提示找不到聲明文件(接上一個問題)

示例代碼

import CryptoJS from 'crypto-js'; 

錯誤信息

解決方法

在src目錄下修改shims-vue.d.ts聲明文件,在末尾增加一行 declare module 'xxx模塊名';

shims-vue.d.ts文件內(nèi)容如下:

declare module '*.vue' {
    import Vue from 'vue';
    export default Vue;
}
declare module 'crypto-js';

JSON直接解析localStorage值報錯

代碼示例

JSON.parse(window.localStorage.getItem('token'));

錯誤信息

Argument of type ‘string | null’ is not assignable to parameter of type ‘string’.
Type ‘null’ is not assignable to type ‘string’.ts(2345)

解決方法

定義一個指定類型為string的變量接收localStorage值

let token: string | null = window.localStorage.getItem('token');
if (token) {
	JSON.parse(token);
}

初始加載的組件未命名,瀏覽器打開頁面后控制臺報錯

代碼示例

//index.vue
@Component
export default class extends Vue {}
//router.ts
import Index from '@/views/index.vue';
const routes: Array<RouteConfig> = [
    {
        path: '/',
        name: 'index',
        component: Index,
    }
];

錯誤信息

Invalid component name: “_class2”. Component names should conform to valid custom element name in html5 specification.

解決方法

給初始加載的組件命名

//index.vue
@Component({
	name: 'Index'
})
export default class extends Vue {}

初始值未定義類型,后面賦值報錯

代碼示例

export default class extends Vue {
    private search = {
        name: '',
        types: [];
    };
	
    private typesChange(value: string[]) {
        this.search.types = value; //這里報錯
    }
}

錯誤信息

Type ‘string[]’ is not assignable to type ‘never[]’.
Type ‘string’ is not assignable to type ‘never’.

解決方法

給初始賦值類型斷言

export default class extends Vue {
    private search = {
        name: '',
        types: [] as string[]; //這里加斷言
    };
	
    private typesChange(value: string[]) {
        this.search.types = value; 
    }
}

在Vue原型上添加屬性使用時報錯

示例代碼

import Vue from 'vue';
import http from './http';
Vue.prototype.$http = http;
this.$http.post('/test', {}).then(
   (resolve: any) => {
       console.log(resolve);
   },
   (reject: any) => {
       console.log(reject);
   }
);

錯誤信息

解決方法

在src目錄下新建vue.d.ts聲明文件

vue.d.ts文件內(nèi)容如下:

import Vue from 'vue';
declare module 'vue/types/vue' {
    interface Vue {
        $http: any;
    }
}

element-ui使用$message報錯

解決方法

在src目錄下新建vue.d.ts聲明文件

vue.d.ts文件內(nèi)容如下:

import Vue from 'vue';
import { ElMessage } from 'element-ui/types/message';
declare module 'vue/types/vue' {
    interface Vue {
        $message: ElMessage;
    }
}

vue-cli里使用process對象報錯類型找不到

解決方法

修改項目根目錄下的tsconfig.json文件中的compilerOptions.types值,新增node

compilerOptions.types配置內(nèi)容如下:

"compilerOptions": {
    "types": ["webpack-env", "node"],
}

vue-cli里tsconfig.json文件報錯

錯誤信息

JSON schema for the typescript compiler's configuration file.
cannot find type definition file for 'webpack-env'.

解決方法

沒找到好的解決方法,偶然間嘗試了下面的方法居然就不報錯了,這種方法不一定適用所有人的項目

修改項目根目錄下的tsconfig.json文件中的compilerOptions.types值,先新增"nodejs",再刪除"nodejs"

先新增:

"compilerOptions": {
    "types": ["webpack-env", "nodejs"],
}

再刪除:

"compilerOptions": {
    "types": ["webpack-env"],
}

邊踩坑,邊更新。。。

————————————分割線————————————

tsconfig.json配置解釋

{
    "compilerOptions": {
        "noEmitOnError": true // 編譯的源文件中存在錯誤的時候不再輸出編譯結(jié)果文件
    }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue數(shù)字相加、相減精度丟失處理3種方法

    Vue數(shù)字相加、相減精度丟失處理3種方法

    這篇文章主要給大家介紹了關(guān)于Vue數(shù)字相加、相減精度丟失處理3種方法的相關(guān)資料,前端在操作加減乘除計算時,經(jīng)常會出現(xiàn)精度缺失問題,有時會顯示為科學計數(shù)的樣式,需要的朋友可以參考下
    2023-08-08
  • vue-router+vuex addRoutes實現(xiàn)路由動態(tài)加載及菜單動態(tài)加載

    vue-router+vuex addRoutes實現(xiàn)路由動態(tài)加載及菜單動態(tài)加載

    本篇文章主要介紹了vue-router+vuex addRoutes實現(xiàn)路由動態(tài)加載及菜單動態(tài)加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 基于vue實現(xiàn)swipe輪播組件實例代碼

    基于vue實現(xiàn)swipe輪播組件實例代碼

    本篇文章主要介紹了基于vue實現(xiàn)swipe輪播組件實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 用vscode開發(fā)vue應用的方法步驟

    用vscode開發(fā)vue應用的方法步驟

    這篇文章主要介紹了用vscode開發(fā)vue應用的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 關(guān)于vue-property-decorator的基礎(chǔ)使用實踐

    關(guān)于vue-property-decorator的基礎(chǔ)使用實踐

    這篇文章主要介紹了關(guān)于vue-property-decorator的基礎(chǔ)使用實踐,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue獲取驗證碼倒計時組件

    vue獲取驗證碼倒計時組件

    這篇文章主要為大家詳細介紹了vue獲取驗證碼倒計時組件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決

    Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決

    這篇文章主要介紹了Vue項目中v-bind動態(tài)綁定src路徑不成功問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 如何利用vue+vue-router+elementUI實現(xiàn)簡易通訊錄

    如何利用vue+vue-router+elementUI實現(xiàn)簡易通訊錄

    這篇文章主要介紹了如何利用vue+vue-router+elementUI實現(xiàn)簡易通訊錄,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • vue axios同步請求解決方案

    vue axios同步請求解決方案

    這篇文章主要介紹了vue axios同步請求解決方案,需要的朋友可以參考下
    2017-09-09
  • Vue模仿ElementUI的form表單實例代碼

    Vue模仿ElementUI的form表單實例代碼

    這篇文章主要給大家介紹了關(guān)于Vue模仿ElementUI的form表單的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03

最新評論

阿拉尔市| 盈江县| 全南县| 平谷区| 壤塘县| 平江县| 宝兴县| 龙海市| 青海省| 英山县| 图片| 五原县| 新龙县| 黔西县| 靖安县| 华宁县| 万源市| 贡山| 兰坪| 江安县| 裕民县| 惠东县| 济宁市| 彰化市| 宣化县| 海南省| 普格县| 克山县| 克拉玛依市| 拉萨市| 广灵县| 工布江达县| 黎川县| 寿光市| 基隆市| 田林县| 四会市| 阿图什市| 梁山县| 红桥区| 龙泉市|