vue3+ts使用过程中的小问题

vue3 中 setup 的使用

问题

  1. 想要在 setup 中使用 this.$message.success()
    想要在 setup 中拿到 this,或者说是拿到组件实例是行不通的

    由于在执行 setup 时,组件实例尚未被创建,因此在 setup 选项中没有 this。这意味着,除了 props 之外,你将无法访问组件中声明的任何属性——本地状态、计算属性或方法。

解决

1. 可以通过引入 elMessge 然后直接使用
import {elMessage} from 'element-plus'
elMessage.success('发送请求成功')
2. getCurrentInstance()来拿到当前组件的实例(注意该方法任然有个问题就是,getCurrentInstance 这个方法只是在 develop(开发模式下)有效,生产环境无法正常使用)

需要注意的是,这个方法只能在 setup 下直接调用或者生命周期中调用才生效,否则返回的是 undefined

setup(){
  // 通过调用该方法拿到实例
  const currentInstance=getCurrentInstance()
  function fetchLists(){
    getCurrentInstance()// don't work
    currentInstance.appContext.config.globalProperties.$message.success()
  }
}
3.可以通过高阶组件(provide/inject)来实现

main.ts 文件下

下面只是一些关键的代码
import App from 'xxxx/App.vue'
import {elMessage} from 'element-plus'
const app=CreateApp(App)
app.provide('$message',elMessage)
app.mounted('#app')

addArticle.vue 文件下

import {inject} from 'vue'
setup(){
  function fetchLists(){
    (inject('$message') as any).success('请求成功')
  }
}

--------------------------------------------------------------------------2021/5/25 分割线------------------------------------------------------------------

在项目中遇到 eslint 报错

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YDUoHfhk-1642137494526)(./images/avatar.jpg)]

Failed to compile.

src/utils/markdown.ts:9:21
TS2322: Type 'string' is not assignable to type 'never'.
     7 |   add: function (text: any, level: any) {
     8 |     var anchor = `#toc${level}${++this.index}`;
  >  9 |     this.toc.push({ anchor: anchor, level: level, text: text });
       |                     ^^^^^^
    10 |     return anchor;
    11 |   },
    12 |   // 使用堆栈的方式处理嵌套的ul,li,level即ul的嵌套层次,1是最外层

需要在 tsconfig.json 中添加以下代码:

    // 忽略 this 的类型检查, Raise error on this expressions with an implied any type.
    "noImplicitThis": false,

所有配置代码如下

    <!-- 所有配置代码如下 -->
    {
    "compilerOptions": {
      "target": "esnext",
      "module": "esnext",
      "strict": true,
      "jsx": "preserve",
      "importHelpers": true,
      "moduleResolution": "node",
      "skipLibCheck": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "sourceMap": true,
      // 忽略 this 的类型检查, Raise error on this expressions with an implied any type.
      "noImplicitThis": false,
      "baseUrl": ".",
      "types": [
        "webpack-env"
      ],
      "paths": {
        "@/*": [
          "src/*"
        ]
      },
      "lib": [
        "esnext",
        "dom",
        "dom.iterable",
        "scripthost"
      ]
    },
    "include": [
      "src/**/*.ts",
      "src/**/*.tsx",
      "src/**/*.vue",
      "tests/**/*.ts",
      "tests/**/*.tsx"
    ],
    "exclude": [
      "node_modules"
    ]
}

vue3 中修改.vue 文件热更新失败的问题

在 vue.config.ts 中配置

module.exports={
  chainWebpack:(config)=>{
    config.resolve.symlinks(true)
  }
}

Vue3 中使用$refs

因为 Vue3 中都是偏向函数式开发所以需要引入 ref


import { ref } from 'vue'

setup() {
  let boxRef = ref(null)
  return {
    boxRef
  }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 + TypeScript项目使用,您可以按照以下步骤进行操作: 1. 安装Vue CLI:如果您还没有安装Vue CLI,可以使用以下命令进行安装: ``` npm install -g @vue/cli ``` 2. 创建项目:使用Vue CLI创建一个新的Vue 3项目,可以运行以下命令: ``` vue create my-project ``` 3. 选择配置:在创建项目的过程,您将被提示选择一些配置选项。请确保选择TypeScript作为您的项目的语言。 4. 安装依赖:项目创建完成后,进入项目目录并安装依赖项。在命令行运行以下命令: ``` cd my-project npm install ``` 5. 创建Vue组件:现在,您可以开始在Vue 3 + TypeScript项目创建组件。打开 src 目录,并创建一个新的 .vue 文件,例如 `HelloWorld.vue`。 ```vue <template> <div> <h1>{{ greeting }}</h1> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ data() { return { greeting: 'Hello, World!' }; } }); </script> ``` 6. 使用组件:在您的应用程序使用该组件,在 `App.vue` 导入并注册 `HelloWorld.vue` 组件。 ```vue <template> <div id="app"> <HelloWorld /> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import HelloWorld from './components/HelloWorld.vue'; export default defineComponent({ name: 'App', components: { HelloWorld } }); </script> ``` 现在,您可以运行您的Vue 3 + TypeScript项目并使用该组件了。运行以下命令启动开发服务器: ``` npm run serve ``` 这只是一个简单的示例,您可以根据自己的需求在项目使用更多的Vue组件和功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值