vue3使用Element-plus与TS(TypeScript)

如果你有一个困扰就是为什么直接CV Element plus文档里的代码总是报各种错误?!那你看这篇就对啦!(针对Vue3哦)

1.项目导入vscode后,安装Element-plus

npm install element-plus --save

 2.在main.js中引入Element-plus

// 导入element-plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
 
app.use(ElementPlus)

3.引入Typescript包(以下3-6步是针对项目已经创建了,划到最底下是在创建项目时用vue-cli创建

npm install --save-dev typescript
npm install --save-dev ts-loader

4.webpack配置

vue cli 3.0及以上版本创建的项目需要在vue.config.js中配置,代码如下:

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true
})
module.exports = { configureWebpack: {
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".json"],  
     alias: {}
  },
  module: {        
    rules: [    
      {    
        test: /\.tsx?$/,    
        loader: 'ts-loader',    
        exclude: /node_modules/,    
        options: {
          appendTsSuffixTo: [/\.vue$/],    
        }    
      }        
    ]    
  }    
}
}

其他的是在webpack.base.conf,可以参考

https://www.jianshu.com/p/3cbcdd766295

5.根目录下创建tsconfig.json,代码如下

{
    "compilerOptions": {
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "lib": ["dom","es2016"],
      "target": "es5"
    },
    "include": ["./src/**/*"]  
}

 6.src下创建test.ts文件,内容为空,如上图

7.另一种办法!用vue-cli引入。官方文档有说明:

就是选择手动搭建,在这一步勾选(空格键),然后回车下一步。 

Over 从此element模版随便cv随便用~

制作不易,欢迎点赞收藏哦!

参考文档

Vue-cli3中使用TS语法,以及使用例子 - 简书

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
Vue 3和TypeScript结合使用Element Plus是很常见的。下面是一些步骤来帮助您在Vue 3项目中使用Element Plus和TypeScript。 1. 创建Vue 3项目:使用Vue CLI或手动创建Vue 3项目。 2. 安装Element Plus:在项目根目录下运行以下命令来安装Element Plus和其它必需的依赖: ```bash npm install element-plus@next ``` 3. 配置Element Plus:在main.ts(或您的入口文件)中引入Element Plus的样式和组件: ```typescript import { createApp } from 'vue'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; import App from './App.vue'; const app = createApp(App); app.use(ElementPlus); app.mount('#app'); ``` 4. 使用Element Plus组件:现在您可以在Vue组件中使用Element Plus的组件了。例如,创建一个Button组件: ```vue <template> <el-button type="primary">Click me</el-button> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'ButtonComponent', }); </script> ``` 5. 使用TypeScript类型检查:在Vue组件中使用TypeScript类型检查非常有用。您可以为Element Plus组件使用TypeScript类型定义,以获得更好的开发体验。例如,使用TypeScript定义Button组件: ```typescript import { defineComponent } from 'vue'; import { ElButton } from 'element-plus'; export default defineComponent({ name: 'ButtonComponent', components: { ElButton, }, }); ``` 这样,您就可以在Vue 3项目中使用Element Plus和TypeScript了。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天又有什么bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值