欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

Monaco-editor,一個vs code 編輯器,需要將其集成到項目。不說閑話了,直接上代碼。 

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']
      })
    ]
  })
}

封裝: 

 首先先封裝個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)建實例
  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',
      // 滾動條
      scrollbar: {
        verticalScrollbarSize: 8,
        horizontalScrollbarSize: 8
      },
      // 行號
      lineNumbers: 'on',
      // tab大小
      tabSize: 2,
      //字體大小
      fontSize: 16,
      // 控制編輯器在用戶鍵入、粘貼、移動或縮進(jìn)行時是否應(yīng)自動調(diào)整縮進(jìn)
      autoIndent: 'advanced',
      // 自動布局
      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)
  }
  // 獲取實例
  function getEditor() {
    return monacoEditor
  }
  // 頁面離開 銷毀
  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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue進(jìn)階之利用transition標(biāo)簽實現(xiàn)頁面跳轉(zhuǎn)動畫

    Vue進(jìn)階之利用transition標(biāo)簽實現(xiàn)頁面跳轉(zhuǎn)動畫

    這篇文章主要為大家詳細(xì)介紹了Vue如何利用transition標(biāo)簽實現(xiàn)頁面跳轉(zhuǎn)動畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起一下
    2023-08-08
  • 使用vue + less 實現(xiàn)簡單換膚功能的示例

    使用vue + less 實現(xiàn)簡單換膚功能的示例

    下面小編就為大家分享一篇使用vue + less 實現(xiàn)簡單換膚功能的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Vue表格組件Vxe-table使用技巧總結(jié)

    Vue表格組件Vxe-table使用技巧總結(jié)

    這篇文章主要給大家介紹了關(guān)于Vue表格組件Vxe-table使用技巧的相關(guān)資料,文中還介紹了VXEtable展示指定行所遇到得問題,對大家學(xué)習(xí)或者使用Vxe-table具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-09-09
  • vue3 定義使用全局變量的示例詳解

    vue3 定義使用全局變量的示例詳解

    全局變量(函數(shù)等)可以在任意組件內(nèi)訪問,可以當(dāng)組件間的傳值使用,這篇文章給大家介紹vue3 定義使用全局變量的示例詳解,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • vue3解構(gòu)賦值失去響應(yīng)式引發(fā)的問題思考

    vue3解構(gòu)賦值失去響應(yīng)式引發(fā)的問題思考

    這篇文章主要介紹了vue3解構(gòu)賦值失去響應(yīng)式引發(fā)的問題思考,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-06-06
  • Vue生產(chǎn)環(huán)境調(diào)試的方法步驟

    Vue生產(chǎn)環(huán)境調(diào)試的方法步驟

    開發(fā)環(huán)境下Vue會提供很多警告來幫你對付常見的錯誤與陷阱,而在生產(chǎn)環(huán)境下,這些警告語句卻沒有用,反而會增加應(yīng)用的體積,下面這篇文章主要給大家介紹了關(guān)于Vue生產(chǎn)環(huán)境調(diào)試的方法步驟,需要的朋友可以參考下
    2022-04-04
  • vue3中update:modelValue的使用與不生效問題解決

    vue3中update:modelValue的使用與不生效問題解決

    現(xiàn)在vue3的使用越來越普遍了,vue3這方面的學(xué)習(xí)我們要趕上,下面這篇文章主要給大家介紹了關(guān)于vue3中update:modelValue的使用與不生效問題的解決方法,需要的朋友可以參考下
    2022-03-03
  • vue實現(xiàn)右側(cè)滑出層動畫

    vue實現(xiàn)右側(cè)滑出層動畫

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)右側(cè)滑出層動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • vue調(diào)試工具vue-devtools的安裝全過程

    vue調(diào)試工具vue-devtools的安裝全過程

    這篇文章主要介紹了vue調(diào)試工具vue-devtools的安裝全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • VUE v-bind 數(shù)據(jù)綁定的示例詳解

    VUE v-bind 數(shù)據(jù)綁定的示例詳解

    這篇文章主要介紹了VUE v-bind 數(shù)據(jù)綁定,簡單點(diǎn)來說就是對 HTML 中的元素,我們可以使用 v-bind 來進(jìn)行綁定和動態(tài)的數(shù)據(jù)輸出,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05

最新評論