全局 API

createApp()

创建应用实例 、第一个参数是 根组件 通常我们都是用app.vue, 第二个参数支持传入根组件的props属性
function createApp(rootComponent: Component, rootProps?: object): App

  1. // main.ts
  2. import { createApp } from "vue";
  3. import App from "App.vue";
  4. const app = createApp(App,{
  5. name:"gf"
  6. })
  7. app.config.errorHandler = (err) => {
  8. /* 处理错误 */
  9. }
  10. // App.vue
  11. <script setup lang="ts">
  12. import { defineProps } from 'vue'
  13. const props = defineProps({
  14. name: String
  15. })
  16. console.log(props) // { name: "gf" }
  17. </script>

app实例属性方法

app.mount()

将应用实例挂载到某个dom节点下

  1. // html
  2. <body>
  3. <div id="app"></div>
  4. <div id="app2"></div>
  5. <script type="module" src="/src/main.ts"></script>
  6. </body>
  7. // main.ts
  8. import { createApp } from "vue"
  9. import App from "App.vue"
  10. const app = createApp(App)
  11. app.mount('#app')
  12. // 挂载多个节点
  13. const buttonText = ref('按钮文案')
  14. const app2 = createApp({
  15. render: () => {
  16. return h(
  17. 'div',
  18. {
  19. style: {
  20. width: '100px',
  21. height: '30px',
  22. background: 'red',
  23. },
  24. onClick: () => {
  25. buttonText.value = '修改按钮文案'
  26. },
  27. },
  28. [h('button', buttonText.value)]
  29. )
  30. },
  31. })
  32. app2.mount('#app2')

app.unmount()

将应用实例卸载

  1. // main.ts
  2. import { createApp, h, ref } from 'vue'
  3. const buttonText = ref('按钮文案')
  4. const app2 = createApp({
  5. render: () => {
  6. return h(
  7. 'div',
  8. {
  9. style: {
  10. width: '100px',
  11. height: '30px',
  12. background: 'red',
  13. },
  14. onClick: () => {
  15. // 卸载应用
  16. app2.unmount()
  17. },
  18. },
  19. [h('button', buttonText.value)]
  20. )
  21. },
  22. })
  23. app2.mount('#app2')

app.provide()

向所有子代组件注入内容、子代组件通过 inject 接收 链接

  1. // main.ts
  2. import { createApp } from 'vue'
  3. import App from './views/app.vue'
  4. const app = createApp(App)
  5. app.provide('rootData', { mes: '根节点注入的provide' })
  6. app.mount('#app')
  7. // app.vue
  8. <script setup lang="ts">
  9. import { inject } from 'vue'
  10. import children from './children.vue'
  11. const rootData = inject('rootData')
  12. console.log('rootData', rootData)
  13. </script>
  14. <template>
  15. <children></children>
  16. </template>
  17. // children.vue
  18. <template>
  19. <div class="com" style="width: 100px; height: 100px; background: red">页面2</div>
  20. </template>
  21. <script setup lang="ts">
  22. import { inject } from 'vue'
  23. const rootData = inject('rootData')
  24. console.log('rootData', rootData)
  25. </script>

app.component()

注入 全局组件 链接

  1. // main.ts
  2. import { createApp } from "vue";
  3. import App from "App.vue";
  4. import MyButton from "../button.vue";
  5. const app = createApp(App);
  6. app.component("MyButton", MyButton);
  7. app.mount("#app")
  8. // App.vue
  9. <template>
  10. <div>
  11. <MyButton></MyButton>
  12. </div>
  13. </template>

app.directive()

注入 全局指令 链接

  1. // main.ts
  2. import { createApp } from 'vue'
  3. import type { DirectiveBinding } from 'vue'
  4. import App from './views/app.vue'
  5. const app = createApp(App)
  6. app.directive('my-directive', {
  7. beforeMount(el: HTMLElement, binding: DirectiveBinding) {
  8. el.innerText = binding.value
  9. },
  10. unmounted() {
  11. console.log('节点被卸载')
  12. },
  13. })
  14. app.mount('#app')
  15. // App.vue
  16. <script setup lang="ts">
  17. import { ref } from 'vue';
  18. const v = ref('button文案')'
  19. </script>
  20. <template>
  21. <div>
  22. app页面
  23. <button v-my-directive="v"></button>
  24. </div>
  25. </template>

app.use()

使用一个 插件 , 插件的理解是你可以做一些插件包给别的开发者使用、插件的作用在于可以在整个工程中做一些全局性的注入! 链接

  1. // 写一个插件、注入全局color指令
  2. // main.ts
  3. import { createApp } from 'vue'
  4. import App from './views/app.vue'
  5. import Color from 'plugin-color'
  6. const app = createApp(App)
  7. app.use(Color)
  8. app.mount('#app')
  9. // plugin-color.js
  10. import type { App, DirectiveBinding } from 'vue'
  11. function colorPlugn() {
  12. const _install = (app: App, options?: { defaultColor: string }) => {
  13. app.directive('color', {
  14. beforeMount(el: HTMLElement, binding: DirectiveBinding) {
  15. el.style.color = binding.value ?? options?.defaultColor
  16. },
  17. })
  18. }
  19. return {
  20. install: _install,
  21. }
  22. }
  23. // app.vue
  24. <script setup lang="ts">
  25. const color = 'red'
  26. </script>
  27. <template>
  28. <div>
  29. app页面
  30. <span v-color>默认颜色</span>
  31. <span v-color="color">红色</span>
  32. </div>
  33. </template>

app.version()

获取vue版本、一般用于版本判断

  1. import { createApp } from 'vue'
  2. import App from './views/app.vue'
  3. const app = createApp(App)
  4. console.log('vue版本', app.version)
  5. app.mount('#app')

app.config()

vue配置选项、vue2在原型挂载内容到vue3都挂载到app.config里面、还包括vue3一些编译配置项设置、自定义元素配置等等….

app.config.globalProperties()

配置全局属性方法、小尤目前推荐使用provide方式使用全局属性方法 链接

  1. // main.ts
  2. import { createApp } from 'vue'
  3. import App from './views/app.vue'
  4. const app = createApp(App)
  5. app.config.globalProperties.globalName = '张三'
  6. app.config.globalProperties.getGlobalName = function () {
  7. return app.config.globalProperties.globalName
  8. }
  9. app.mount('#app')
  10. // app.vue
  11. <script setup lang="ts">
  12. import { getCurrentInstance } from 'vue'
  13. import type { proxyRefs } from 'vue'
  14. const { proxy } = getCurrentInstance()
  15. console.log(proxy.globalName)
  16. </script>
  17. <template>
  18. <div>app页面</div>
  19. </template>

app.compilerOptions = {…}

编译配置选项

  1. interface ComponentOptions {
  2. compilerOptions?: {
  3. isCustomElement?: (tag: string) => boolean // 判断自定义元素配置
  4. whitespace?: 'condense' | 'preserve' // 默认:'condense' 用于调整模板中空格的处理行为
  5. delimiters?: [string, string] // 默认:['{{', '}}'] 用于调整模板内文本插值的分隔符。
  6. comments?: boolean // 默认:false
  7. }
  8. }

组合式API

定义props

使用 withDefaults() 可以定义默认值

  1. // button.vue
  2. <template>
  3. <div style="width: 100px; height: 100px; background: red">
  4. {{ props.name }}
  5. </div>
  6. </template>
  7. <script setup lang="ts">
  8. import { defineProps, withDefaults } from 'vue'
  9. // 定义Props
  10. const props = withDefaults(
  11. defineProps<{
  12. name: string
  13. age?: number
  14. }>(),
  15. { age: 18 } //定义默认值
  16. )
  17. </script>
  18. // app.vue
  19. <script setup lang="ts">
  20. import MyButton from './button.vue'
  21. </script>
  22. <template>
  23. <MyButton name="张三"></Button>
  24. <span>{{ props.age }}</span>
  25. </template>

子组件派发事件

  1. // Mybutton.vue
  2. <template>
  3. <button @click="displayMes">派发事件</button>
  4. </template>
  5. <script setup lang="ts">
  6. import { defineEmits } from 'vue'
  7. // 定义emit
  8. const emit = defineEmits<{
  9. (e: 'getName', v: string): void
  10. }>()
  11. // 派发事件
  12. function displayMes() {
  13. emit('getName', props.name)
  14. }
  15. </script>
  16. //app.vue
  17. <script setup lang="ts">
  18. import MyButton from './my-button.vue'
  19. function myButtonGetName(v: string) {
  20. console.log(v)
  21. }
  22. </script>
  23. <template>
  24. <MyButton @getName="myButtonGetName"></MyButton>
  25. </template>

父组件获取子组件实例

这里需要通过defineExpose导出去的才能被父组件拿到

  1. // MyButton.vue
  2. <template>
  3. <div style="width: 100px; height: 100px; background: red">
  4. <button @click="displayMes">派发事件</button>
  5. </div>
  6. </template>
  7. <script setup lang="ts">
  8. function getName() {
  9. console.log('ref调用方式', '李四')
  10. }
  11. // 定义导出的方式
  12. defineExpose({
  13. getName,
  14. name:'李四'
  15. })
  16. </script>
  17. // app.vue
  18. <script setup lang="ts">
  19. import { ref } from 'vue'
  20. import MyButton from './my-button.vue'
  21. const myButtonRef = ref()
  22. function getMybutton() {
  23. myButtonRef.value.getName() // 'ref调用方式', '李四'
  24. console.log(myButtonRef.value.name) //李四
  25. }
  26. </script>
  27. <template>
  28. <MyButton ref="myButtonRef"></MyButton>
  29. <button @click="getMybutton">获取子组件实例</button>
  30. </template>

useSlots和useAttrs

  1. <script setup lang="ts">
  2. import { useSlots, useAttrs } from 'vue'
  3. const slots = useSlots()
  4. const attrs = useAttrs()
  5. </script>