Carmineprince's Blog
首页
    • HTML
    • CSS
    • JavaScript
    • Vue
    • React
    • TypeScript
    • Node
    • Flutter
    • Electron
    • Python
    • 运维
    • 重学前端
  • 分类
  • 标签
  • 归档

Ziqi Wang

胡思乱想程序员
首页
    • HTML
    • CSS
    • JavaScript
    • Vue
    • React
    • TypeScript
    • Node
    • Flutter
    • Electron
    • Python
    • 运维
    • 重学前端
  • 分类
  • 标签
  • 归档
  • 从0开始搭建一套规范的Vue3.x项目工程环境
  • 解决vite构建vue3项目报错
  • 解决Vue3中无法使用$ref的问题
  • 项目中手动增加scss支持
  • el-table多选分页保留
  • 解决npm run dev启动两个页面问题
  • PC端和移动端兼容方案
  • SFC-Use-script-setup
    • 基础语法
    • 响应式
    • 使用组件
    • 动态组件
    • 重命名组件
    • 命名空间的组件
      • `defineProps`
      • TypeScript props declarations
      • defineEmits
      • TypeScript Emits declarations
    • 全局变量-使用`Vue.prototype`替代方案
      • 方案一:`globalProperties`(不使用Composition Api的话推荐使用,否则不推荐)
      • 方案二 Provide / Inject
  • vue2x面试前回炉重造
  • Vue3+Vite2配置
  • flvjs+vue 视频播放(手动追帧、断开重连、多个视频同时直播)
  • vue平滑滚动到指定位置
  • vant项目使用postcss-pxtorem和amfe-flexible 进行移动端适配
  • vue 使用v-html绑定时,内部元素不会继承外部css的解决方案
  • vue-awesome-swiper缩略图控制循环无效解决方案
  • 如何在Vue项目中优雅的使用svg图标
  • yarn报错及解决方案
  • vue3+ts+vite移动端vw适配方案
  • Vue项目中增加mockjs模拟接口请求
  • Nuxt相关

  • Vue3思考及跟Vue2相比的新变化总结
  • Vue相关
carmineprince
2021-10-20

SFC-Use-script-setup

# SFC使用script setup

[TOC]

# 基础语法

<script setup>
// variable
const msg = 'Hello!'

// functions
function log() {
  console.log(msg)
}

// import 
import { capitalize } from './helpers'
</script>

<template>
  <div @click="log">{{ msg }}</div>

  <div>{{ capitalize('hello') }}</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 响应式

<script setup>
import { ref } from 'vue'

const count = ref(0)
</script>

<template>
  <button @click="count++">{{ count }}</button>
</template>

1
2
3
4
5
6
7
8
9
10

# 使用组件

<script setup>
import MyComponent from './MyComponent.vue'
</script>

<template>
  <MyComponent />
</template>
1
2
3
4
5
6
7

# 动态组件

<script setup>
import Foo from './Foo.vue'
import Bar from './Bar.vue'
</script>

<template>
  <component :is="Foo" />
  <component :is="someCondition ? Foo : Bar" />
</template>
1
2
3
4
5
6
7
8
9

# 重命名组件

import { FooBar as FooBarChild } from './components'
1

# 命名空间的组件

<script setup>
import * as Form from './form-components'
</script>

<template>
  <Form.Input>
    <Form.Label>label</Form.Label>
  </Form.Input>
</template>
1
2
3
4
5
6
7
8
9

# defineProps

<script setup>
const props = defineProps({
  foo: String
})
</script>
1
2
3
4
5

# TypeScript props declarations

// Parent Component
<template>
  <home-item title="按钮" :disabled="true"></home-item>
</template>
<script setup lang="ts">
import HomeItem from './HomeItem.vue'
</script>
1
2
3
4
5
6
7
// Child Component
<template>
  <el-button :disabled="disabled">{{ title }}</el-button>
</template>
<script setup lang="ts">
import { defineProps, withDefaults } from 'vue'

interface Props {
  title?: string
  disabled?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  title: '按钮',
  disabled: false
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# defineEmits

<script setup>
const emit = defineEmits(['change', 'delete'])
// setup code
</script>
1
2
3
4

# TypeScript Emits declarations

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

const count = ref(0)
type Emits = {
  (e: 'click', msg: string): void
  (e: 'change', num: number): void
}
const emit = defineEmits<Emits>()
const handleClick = (): void => {
  emit('click', 'Leave a message is necessary')
}
const handleAddClick = (): void => {
  count.value += 1
  emit('change', count.value)
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 全局变量-使用Vue.prototype替代方案

在Vue 2.x中可使用this.直接使用 ,但是在Vue 3.x中无法使用Vue.prototype进行设置,并且如果使用composition Api 没有this。那我们应该如何使用全局变量呢?

// main.js中
import Tool from '@/utils/tool'
Vue.prototype.$tool = Tool
// xxx.vue中使用this.直接使用
this.$tool.showValue(NaN,0)
1
2
3
4
5

# 方案一:globalProperties(不使用Composition Api的话推荐使用,否则不推荐)

  • 使用globalProperties定义

    // main.js或main.ts中
    const app = createApp({})
    app.config.globalProperties.$tool = Tool
    
    1
    2
    3
  • Option Api: Vue3.x的使用方式同Vue 2.x

    this.$tool.showValue(NaN,0)
    
    1
  • Composition Api : 使用getCurrentInstance替代this(官方不推荐使用该方式),另外不要用ctx获取,可能会造成生产环境异常。

    <script lang="ts">
    import { defineComponent, getCurrentInstance } from 'vue'
    
    export default defineComponent({
      setup() {
        const { proxy } = getCurrentInstance()
        console.log(proxy.$tool.showValue(NaN, 0)) // 0
      }
    })
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
  • <script setup>:使用getCurrentInstance替代this(官方不推荐使用该方式),另外不要用ctx获取,可能会造成生产环境异常。

    <script setup lang="ts">
    import { inject } from 'vue'
    
    const { proxy } = getCurrentInstance()
    console.log(proxy.$tool.showValue(NaN,0)) // 0
    </script>
    
    1
    2
    3
    4
    5
    6

# 方案二 Provide / Inject

  • Provide
// main.js或main.ts中
const app = createApp({})
app.provide('$tool', Tool)
1
2
3
  • Inject
// script setup
<template>{{ $tool.showValue(NaN, 0) }}</template>
<script setup lang="ts">
import { inject } from 'vue'

const $tool = inject('$tool')
</script>
1
2
3
4
5
6
7

但是:这样跟每个组件里面直接引入文件也没有什么区别,所以还是直接引入吧。可以全局用globalProperties配置在使用Option API时使用。其余情况直接手动引入公共类吧。这样最起码在<templete/>中可以直接使用

#vue3#setup#SFC
上次更新: 10/21/2021, 1:52:09 PM
PC端和移动端兼容方案
vue2x面试前回炉重造

← PC端和移动端兼容方案 vue2x面试前回炉重造→

最近更新
01
pc端rem配置
03-02
02
使用动态变量ts报错的解决
02-25
03
React Hook详解
02-18
更多文章>
Theme by Vdoing | Copyright © 2021-2022 Carmineprince | 鲁ICP备2021046263号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式