【问题标题】:What is the best solution to build a vue3 + ts + vite project搭建vue3 + ts + vite项目的最佳方案是什么
【发布时间】:2022-01-11 13:17:44
【问题描述】:

我是vue新手,对如何正确组合vite,ts和vue3感到困惑。我想知道vue中最好的解决方案是什么,我稍后会添加vue route,vuex。

我找到了两种创建上述项目的方法:

  1. npm init vite(那我选择vue-ts)
  2. npm init vite-app <project-name>(然后我使用 npm 手动添加 typescript)

我的问题是:

  1. 第一种方式生成vite.config.ts,而第二种方式没有,为什么,最好的解决方案是什么?

  2. 对于ts在vue中的代码风格,写什么风格比较好。我见过很多种在vue3中写一个组件的代码 用打字稿,例如:

(1)

<script>
  import { defineComponent, computed } from 'vue'
  import { useStore } from 'vuex'
  import { key } from '../store'

  export default defineComponent({
    name: 'HelloWorld',
    props: {
      msg: {
        type: String,
        default: ''
      }
    },
    setup() {
      const store = useStore(key)
      const count = computed(() => store.state.count)

      return {
        count,
        inCrement: () => store.commit('increment')
      }
    }
  })
</script>

(2)

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
import { isExternal } from '@/utils/validate'

@Component({
  name: 'SidebarItemLink'
})
export default class extends Vue {
  @Prop({ required: true }) private to!: string

  private isExternal = isExternal
}
</script>

【问题讨论】:

  • 可能不相关,但 NuxtJS 是一个构建在 Vue 之上的框架。它内置了对 Typescript 和 Vite 的支持。自己去看看,也许你能从中得到一些很好的启发; nuxtjs.org
  • 谢谢,我去看看这个文档。

标签: typescript vue.js vue-component vite


【解决方案1】:

对于组件代码风格,vue 3推荐使用defineComponent + composition api + template(or tsx)

<template>
  <p class="msg">{{ msg }}</p>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  setup () {
    const msg = 'Hello World!';

    return {
      msg
    }
  }
})
</script>

<style scoped lang="scss" >

</style>

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 2021-06-05
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    相关资源
    最近更新 更多