最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3中如何使用component :is 加載組件

 更新時(shí)間:2023年11月03日 10:25:52   作者:會(huì)說(shuō)法語(yǔ)的豬  
Monaco-editor,一個(gè)vs code 編輯器,需要將其集成到項(xiàng)目,這篇文章主要介紹了Vue3中如何使用component :is 加載組件,需要的朋友可以參考下

Monaco-editor,一個(gè)vs code 編輯器,需要將其集成到項(xiàng)目。不說(shuō)閑話了,直接上代碼。 

npm地址:https://www.npmjs.com/package/monaco-editor

中文文檔:https://aydk.site/editor/

安裝:

pnpm add monaco-editor -S
pnpm add vite-plugin-monaco-editor -D

配置: 

vite.config.ts

import { defineConfig} from 'vite'
// vs code 編輯器配置
import monacoEditorPlugin from 'vite-plugin-monaco-editor'
// https://vitejs.dev/config/
export default ({ mode }) => {
  return defineConfig({
    plugins: [
      monacoEditorPlugin({
        languageWorkers: ['editorWorkerService', 'typescript', 'json', 'html']
      })
    ]
  })
}

封裝: 

 首先先封裝個(gè)hook如下:

@/hooks/useMonacoEditor.hook.ts 

import * as monaco from 'monaco-editor'
import useCommonStore from '@/store/common'
import { ref, nextTick, onBeforeUnmount } from 'vue'
export function useMonacoEditor(language: string = 'javascript') {
  // 編輯器示例
  let monacoEditor: monaco.editor.IStandaloneCodeEditor | null = null
  // 目標(biāo)元素
  const monacoEditorRef = ref<HTMLElement | null>(null)
  // 創(chuàng)建實(shí)例
  function createEditor(editorOption: monaco.editor.IStandaloneEditorConstructionOptions = {}) {
    if(!monacoEditorRef.value) return
    monacoEditor = monaco.editor.create(monacoEditorRef.value, {
      // 初始模型
      model: monaco.editor.createModel('', language),
      // 是否啟用預(yù)覽圖
      minimap: { enabled: true },
      // 圓角
      roundedSelection: true,
      // 主題
      theme: useCommonStore().mode == 'dark' ? 'vs-dark' : 'vs',
      // 主鍵
      multiCursorModifier: 'ctrlCmd',
      // 滾動(dòng)條
      scrollbar: {
        verticalScrollbarSize: 8,
        horizontalScrollbarSize: 8
      },
      // 行號(hào)
      lineNumbers: 'on',
      // tab大小
      tabSize: 2,
      //字體大小
      fontSize: 16,
      // 控制編輯器在用戶鍵入、粘貼、移動(dòng)或縮進(jìn)行時(shí)是否應(yīng)自動(dòng)調(diào)整縮進(jìn)
      autoIndent: 'advanced',
      // 自動(dòng)布局
      automaticLayout: true,
      ...editorOption
    })
    return monacoEditor
  }
  // 格式化
  async function formatDoc() {
    await monacoEditor?.getAction('editor.action.formatDocument')?.run()
  }
  // 數(shù)據(jù)更新
  function updateVal(val: string) {
    nextTick(() => {
      if(getOption(monaco.editor.EditorOption.readOnly)) {
        updateOptions({ readOnly: false })
      }
      monacoEditor?.setValue(val)
      setTimeout(async () => {
        await formatDoc()
      }, 10)
    })
  }
  // 配置更新
  function updateOptions(opt: monaco.editor.IStandaloneEditorConstructionOptions) {
    monacoEditor?.updateOptions(opt)
  }
  // 獲取配置
  function getOption(name: monaco.editor.EditorOption) {
    return monacoEditor?.getOption(name)
  }
  // 獲取實(shí)例
  function getEditor() {
    return monacoEditor
  }
  // 頁(yè)面離開 銷毀
  onBeforeUnmount(() => {
    if(monacoEditor) {
      monacoEditor.dispose()
    }
  })
  return {
    monacoEditorRef,
    createEditor,
    getEditor,
    updateVal,
    updateOptions,
    getOption,
    formatDoc
  }
}

然后調(diào)用上面 useMonacoEditor 封裝editor編輯器組件

@/components/MonacoEditor/index.vue 

<template>
  <div ref="monacoEditorRef" :style="monacoEditorStyle"></div>
</template>
<script setup lang="ts">
import { useMonacoEditor } from '@/hooks'
import { onMounted, computed, watch } from 'vue'
const props = withDefaults(defineProps<{
  width?: string | number,
  height?: string | number,
  language?: string,
  editorOption?: Object,
  modelValue: string
}>(), {
  width: '100%',
  height: '100%',
  language: 'javascript',
  editorOption: () => ({}),
  modelValue: ''
})
const emits = defineEmits<{
  (e: 'blue'): void,
  (e: 'update:modelValue', val: string): void
}>()
const monacoEditorStyle = computed(() => {
  return { 
    width: typeof props.width === 'string' ? props.width : props.width + 'px', 
    height: typeof props.height === 'string' ? props.height : props.height + 'px'
  }
})
const { monacoEditorRef, createEditor, updateVal, updateOptions, getEditor } = useMonacoEditor(props.language)
onMounted(() => {
  const monacoEditor = createEditor(props.editorOption)
  updateMonacoVal(props.modelValue)
  monacoEditor?.onDidChangeModelContent(() => {
    emits('update:modelValue', monacoEditor!.getValue())
  })
  monacoEditor?.onDidBlurEditorText(() => {
    emits('blue')
  })
})
watch(() => props.modelValue, () => {
  updateMonacoVal(props.modelValue)
})
function updateMonacoVal(val: string) {
  if(val !== getEditor()?.getValue()) {
    updateVal(val)
  }
}
defineExpose({ updateOptions })
</script>

組件使用: 

<div class="edit-container">
  <MonacoEditor ref="MonacoEditRef" v-model="editJson" language="json" />
</div>
<script setup lang="ts">
import MonacoEditor from '@/components/MonacoEditor/index.vue'
import { ref } from 'vue'
let editJson = ref('')
const MonacoEditRef = ref<InstanceType<typeof MonacoEditor>>()
//MonacoEditRef.value!.updateOptions({ theme: 'vs' }) 調(diào)用子組件方法修改配置
</script>

到此這篇關(guān)于Vue3中使用component :is 加載組件的文章就介紹到這了,更多相關(guān)Vue3使用component :is 加載組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue 菜單欄點(diǎn)擊切換單個(gè)class(高亮)的方法

    Vue 菜單欄點(diǎn)擊切換單個(gè)class(高亮)的方法

    今天小編就為大家分享一篇Vue 菜單欄點(diǎn)擊切換單個(gè)class(高亮)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Vue微信項(xiàng)目按需授權(quán)登錄策略實(shí)踐思路詳解

    Vue微信項(xiàng)目按需授權(quán)登錄策略實(shí)踐思路詳解

    這篇文章主要介紹了Vue微信項(xiàng)目按需授權(quán)登錄策略實(shí)踐思路詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • Vue中$forceUpdate()的使用方式

    Vue中$forceUpdate()的使用方式

    這篇文章主要介紹了Vue中$forceUpdate()的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vuex實(shí)現(xiàn)購(gòu)物車的增加減少移除

    vuex實(shí)現(xiàn)購(gòu)物車的增加減少移除

    這篇文章主要為大家詳細(xì)介紹了vuex實(shí)現(xiàn)購(gòu)物車的增加減少移除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • vue3中toRef、toRefs和toRaw的使用

    vue3中toRef、toRefs和toRaw的使用

    本文主要介紹了vue3中toRef、toRefs和toRaw的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • vue中的cookies緩存存值方式 超簡(jiǎn)單

    vue中的cookies緩存存值方式 超簡(jiǎn)單

    這篇文章主要介紹了vue中的cookies緩存存值方式,超簡(jiǎn)單!具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Nuxt使用Vuex的方法示例

    Nuxt使用Vuex的方法示例

    這篇文章主要介紹了Nuxt使用Vuex的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 基于Vue.js 2.0實(shí)現(xiàn)百度搜索框效果

    基于Vue.js 2.0實(shí)現(xiàn)百度搜索框效果

    這篇文章主要為大家詳細(xì)介紹了基于Vue.js 2.0實(shí)現(xiàn)百度搜索框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 淺析vue-cli3配置webpack-bundle-analyzer插件【推薦】

    淺析vue-cli3配置webpack-bundle-analyzer插件【推薦】

    小編最近為了優(yōu)化vue項(xiàng)目性能,需要使用webpack-bundle-analyzer插件來(lái)分析報(bào)文件,在網(wǎng)上沒(méi)有找到合適的,下面小編給大家寫出來(lái)一個(gè)供大家參考
    2019-10-10
  • vue3 vite如何讀取文件內(nèi)容

    vue3 vite如何讀取文件內(nèi)容

    這篇文章主要介紹了vue3 vite如何讀取文件內(nèi)容問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評(píng)論

宣武区| 富锦市| 常州市| 易门县| 罗山县| 新龙县| 云林县| 江北区| 崇礼县| 荃湾区| 宣城市| 满洲里市| 石柱| 定陶县| 合江县| 武宁县| 平阴县| 巴彦淖尔市| 曲周县| 湘西| 赣榆县| 方山县| 德钦县| 金堂县| 昌邑市| 青龙| 乳源| 罗山县| 宜川县| 九龙县| 开鲁县| 蒙自县| 灵石县| 利津县| 德昌县| 延吉市| 攀枝花市| 闽侯县| 新乡县| 桃江县| 双鸭山市|