ElementUI/vue-cli-plugin-element

vue3+ts中使用this.$alert编译时报错

chenchensdo opened this issue · 2 comments

Property '$alert' does not exist on type 'HelloWorld'

MMU2 commented

me too, I got a error in vscode

Property '$message' does not exist on type 'xxx'.

but the $message works in chrome

MMU2 commented

解决了,查了ts文档
@types,typeRoots和types这部分
里面有讲到编译时,对于types的指定位置是如何配置的
我的项目用脚手架初始化的,element-ui时后面装的,我发现初始的tsconfig文件下面指定的types文件夹就不包含element-ui,要手动加上

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "mocha",
      "chai",
      "./../element-ui/types" // 加上这个就行
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}