vue3——全局、递归组件

定义一个组件Card

<template>
  <div class="card">
    <header>
      <div>
        标题
      </div>
      <div>副标题</div>
    </header>
    <section>内容</section>
  </div>
</template>

<script setup lang="ts">

</script>

<style lang="scss" scoped>
$border: #ccc;
.card {
  border: 1px solid #{$border};
  width: 400px;
  header {
    display: flex;
    justify-content: space-between;
    padding: 5px;
    border-bottom: 1px solid #{$border};
  }
  section {
    padding: 5px;
    min-height: 300px;
  }
}
</style>

一、全局组件

import { createApp } from 'vue'
import App from './App.vue'
import "@gyk/components/src/assets/styles/bem.scss"
import "@gyk/components/src/assets/styles/reset.scss"
import CardVue from './components/example/Card.vue'

export const app = createApp(App)

// 注册全局组件
app.component("Card", CardVue)


// 挂载在指定节点上
app.mount('#app')

在指定组件中使用

<template>
  <div>
    <Card></Card>
    <Card></Card>
    <Card></Card>
    <Card></Card>
  </div>
</template>

<script setup lang="ts">
// import Card from './components/example/Card.vue'
</script>

<style lang="scss" scoped>

</style>

二、递归组件

<template>
  <div v-for="item in treeData" class="gyk-tree">
    <div>{{ item.name }}<input type="checkbox" v-model="item.checked"/></div>
    <!-- 方式一,直接使用该文件名当作标签使用 -->
    <!-- <Tree v-if="item.children" :treeData="item.children"></Tree> -->
    <gykTree v-if="item.children" :treeData="item.children"></gykTree>
  </div>
</template>

<!-- 方式二:如果想修改标签的名字,可以使用这种方式,再写一个script,但是不能加上setup,也比较繁琐
<script lang="ts">
  export default 
  {
    name: 'gykTree'
  }
</script>
-->

<script setup lang="ts">
// 方式三:引入unplugin-vue-define-options
// 步骤一:pnpm add unplugin-vue-define-options
// 再vite.config.ts中引入,再去ts.config里面添加代码提示
defineOptions({
  name: 'gykTree'
})

type Tree = {
  id: string
  name: string
  checked: boolean
  children?: Tree[]
}

defineProps<{
  treeData: Tree[]
}>()
</script>

<style lang="scss" scoped>
@include b(tree) {
  margin-left: 10px;
}
</style>

方式三中的配置如下
vite.config.ts

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import DefineOptions from 'unplugin-vue-define-options/vite'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    DefineOptions(),
    vue()
  ],
  // 给bem.scss添加为全局的
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
          @import "@gyk/components/src/assets/styles/bem.scss";
          @import "@gyk/components/src/assets/styles/reset.scss";
        `
      }
    }
  }
})

ts.config.json

{
  "compilerOptions": {
    // 提示unplugin插件,用来更改递归组件的名称
    "types": ["unplugin-vue-define-options/macros-global"],
    "target": "es2016",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "lib": ["es2016", "DOM", "DOM.Iterable"],
    "skipLibCheck": true,
    "noImplicitAny": false,
    "experimentalDecorators": true, // 允许使用装饰器
    "emitDecoratorMetadata": true, // 允许装饰器
    "noUnusedLocals": false,                /* 允许未使用的局部变量 */
    "noUnusedParameters": false,            /* 允许未使用的参数 */

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",

    /* Linting */
    "strict": true,
    "noFallthroughCasesInSwitch": false
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值