vue3+typescript element-plus 引入ElMessage, ElMessageBox 样式显示在app标签外部

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue3+typescript element-plus 引入ElMessage, ElMessageBox 样式显示在app标签外部相关的知识,希望对你有一定的参考价值。

参考技术A

局部引入element-plus

下面图片是样式显示在#app标签 外部

解决方法:需要引入element-plus ElMessage,ElMessageBox 的样式

注:以上仅代表个人遇到的问题及解决方法

在vue项目中写Typescript

两种方式:

  • extends写法(不使用装饰器) ---- 更贴近于vue语法
import Vue from \'vue\'
import MainHeader from \'./header.vue\'
import MainContent from \'./content.vue\'

interface User {
  name: string,
  age: number
}

export default Vue.extend({
  components: {
    MainHeader,
    MainContent
  },
  props: {
    testProps: {
      type: Object as () => User
    }
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    setShow (state: boolean): void {
      this.show = state
    }
  },
  computed: {
    num(): number {
      return this.count
    },
    name: {
      // 需要标注有 `this` 参与运算的返回值类型
      get(): string {
        return this.item.n
      },
      set(val: string) {
        this.item.n = val
      }
    }
  }
})
  • vue-property-decorator(装饰器语法) ---- 贴近类的写法
import { Vue, Component, Watch } from \'vue-property-decorator\'
interface KeyValue {
  c: string
  n: string
}

@Component({
  components: {
    MainHeader,
    MainContent
  }
})
export default class Test extends Vue {
  // data
  count: number = 1
  item: KeyValue = {
    c: \'\',
    n: \'\'
  }

  // computed
  get num(): number {
    return this.count
  }
  get name(): string {
    return this.item.n
  }
  // 注意,这里不能标返回值类型,就算写void也不行
  set name(val: string) {
    this.item.n = val
  }

  // watch
  @Watch(\'count\')
  watchCount(newVal: number, oldVal: number): void {
    console.log(newVal)
  }
  @Watch(\'item.n\')
  watchName(newVal: string, oldVal: string): void {
    console.log(newVal)
  }
  @Watch(\'item\', { deep: true })
  watchItem(newVal: KeyValue, oldVal: KeyValue): void {
    console.log(newVal)
  }
  // methods
  reset(): void {
    this.$emit(\'reset\')
  },
  getKey(): string {
    return this.item.c
  }
}

以上是关于vue3+typescript element-plus 引入ElMessage, ElMessageBox 样式显示在app标签外部的主要内容,如果未能解决你的问题,请参考以下文章

Vue3 + TypeScript 开发实践总结

深入Vue3+TypeScript技术栈-coderwhy新课

Vue3+TypeScript完整项目上手教程

vue3+typescript实战记录二(typescript-eslint)

Vue3都要上的TypeScript之工程实践

带有 Typescript 的 Vue3 路由器:缺少 RouteConfig