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

通過npm或yarn自動生成vue組件的方法示例

 更新時間:2019年02月12日 11:29:27   作者:zer0_li  
這篇文章主要介紹了通過npm或yarn自動生成vue組件的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

不知道大家每次新建組件的時候,是不是都要創(chuàng)建一個目錄,然后新增一個.vue文件,然后寫template、script、style這些東西,如果是公共組件,是不是還要新建一個index.js用來導(dǎo)出vue組件、雖然有vscode有代碼片段能實現(xiàn)自動補全,但還是很麻煩,今天靈活運用scripts工作流,自動生成vue文件和目錄。

實踐步驟

安裝一下chalk,這個插件能讓我們的控制臺輸出語句有各種顏色區(qū)分

npm install chalk --save-dev 
yarn add chalk --save-dev

在根目錄中創(chuàng)建一個 scripts 文件夾

新增一個generateComponent.js文件,放置生成組件的代碼

新增一個template.js文件,放置組件模板的代碼

template.js文件,里面的內(nèi)容可以自己自定義,符合當(dāng)前項目的模板即可

// template.js
module.exports = {
 vueTemplate: compoenntName => {
  return `<template>
 <div class="${compoenntName}">
  ${compoenntName}組件
 </div>
</template>

<script>
export default {
 name: '${compoenntName}'
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
.${compoenntName} {

}
</style>

`
 },
 entryTemplate: `import Main from './main.vue'
export default Main`
}

generateComponent.js生成vue目錄和文件的代碼

// generateComponent.js`
const chalk = require('chalk') // 控制臺打印彩色
const path = require('path')
const fs = require('fs')
const resolve = (...file) => path.resolve(__dirname, ...file)
const log = message => console.log(chalk.green(`${message}`))
const successLog = message => console.log(chalk.blue(`${message}`))
const errorLog = error => console.log(chalk.red(`${error}`))
const { vueTemplate, entryTemplate } = require('./template')
const _ = process.argv.splice(2)[0] === '-com'

const generateFile = (path, data) => {
 if (fs.existsSync(path)) {
  errorLog(`${path}文件已存在`)
  return
 }
 return new Promise((resolve, reject) => {
  fs.writeFile(path, data, 'utf8', err => {
   if (err) {
    errorLog(err.message)
    reject(err)
   } else {
    resolve(true)
   }
  })
 })
}

// 公共組件目錄src/base,全局注冊組件目錄src/base/global,頁面組件目錄src/components
_ ? log('請輸入要生成的組件名稱、如需生成全局組件,請加 global/ 前綴') : log('請輸入要生成的頁面組件名稱、會生成在 components/目錄下')
let componentName = ''
process.stdin.on('data', async chunk => {
 const inputName = String(chunk).trim().toString()

 // 根據(jù)不同類型組件分別處理
 if (_) {
  // 組件目錄路徑
  const componentDirectory = resolve('../src/base', inputName)
  // vue組件路徑
  const componentVueName = resolve(componentDirectory, 'main.vue')
  // 入口文件路徑
  const entryComponentName = resolve(componentDirectory, 'index.js')

  const hasComponentDirectory = fs.existsSync(componentDirectory)
  if (hasComponentDirectory) {
   errorLog(`${inputName}組件目錄已存在,請重新輸入`)
   return
  } else {
   log(`正在生成 component 目錄 ${componentDirectory}`)
   await dotExistDirectoryCreate(componentDirectory)
  }

  try {
   if (inputName.includes('/')) {
    const inputArr = inputName.split('/')
    componentName = inputArr[inputArr.length - 1]
   } else {
    componentName = inputName
   }
   log(`正在生成 vue 文件 ${componentVueName}`)
   await generateFile(componentVueName, vueTemplate(componentName))
   log(`正在生成 entry 文件 ${entryComponentName}`)
   await generateFile(entryComponentName, entryTemplate)
   successLog('生成成功')
  } catch (e) {
   errorLog(e.message)
  }
 } else {
  const inputArr = inputName.split('/')
  const directory = inputArr[0]
  let componentName = inputArr[inputArr.length - 1]

  // 頁面組件目錄
  const componentDirectory = resolve('../src/components', directory)

  // vue組件
  const componentVueName = resolve(componentDirectory, `${componentName}.vue`)

  const hasComponentDirectory = fs.existsSync(componentDirectory)
  if (hasComponentDirectory) {
   log(`${componentDirectory}組件目錄已存在,直接生成vue文件`)
  } else {
   log(`正在生成 component 目錄 ${componentDirectory}`)
   await dotExistDirectoryCreate(componentDirectory)
  }

  try {
   log(`正在生成 vue 文件 ${componentName}`)
   await generateFile(componentVueName, vueTemplate(componentName))
   successLog('生成成功')
  } catch (e) {
   errorLog(e.message)
  }
 }

 process.stdin.emit('end')
})

process.stdin.on('end', () => {
 log('exit')
 process.exit()
})

function dotExistDirectoryCreate (directory) {
 return new Promise((resolve) => {
  mkdirs(directory, function () {
   resolve(true)
  })
 })
}

// 遞歸創(chuàng)建目錄
function mkdirs (directory, callback) {
 var exists = fs.existsSync(directory)
 if (exists) {
  callback()
 } else {
  mkdirs(path.dirname(directory), function () {
   fs.mkdirSync(directory)
   callback()
  })
 }
}

配置package.json,scripts新增兩行命令,其中-com是為了區(qū)別是創(chuàng)建頁面組件還是公共組件

"scripts": {
  "new:view":"node scripts/generateComponent",
  "new:com": "node scripts/generateComponent -com"
 },

執(zhí)行

  npm run new:view // 生成頁組件
  npm run new:com // 生成基礎(chǔ)組件
  或者
  yarn run new:view // 生成頁組件
  yarn run new:com // 生成基礎(chǔ)組件


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3中的動態(tài)組件詳解

    Vue3中的動態(tài)組件詳解

    本文介紹了Vue3中的動態(tài)組件,通過`<component :is="動態(tài)組件名或組件對象"></component>`來實現(xiàn)根據(jù)條件動態(tài)渲染不同的組件,此外,還提到了使用`markRaw`和`shallowRef`來優(yōu)化性能,避免不必要的響應(yīng)式劫持
    2025-02-02
  • dataV大屏在vue中的使用方式

    dataV大屏在vue中的使用方式

    這篇文章主要介紹了dataV大屏在vue中的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue用addRoutes實現(xiàn)動態(tài)路由的示例

    vue用addRoutes實現(xiàn)動態(tài)路由的示例

    本篇文章主要介紹了vue用addRoutes實現(xiàn)動態(tài)路由的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue如何根據(jù)角色獲取菜單動態(tài)添加路由

    Vue如何根據(jù)角色獲取菜單動態(tài)添加路由

    這篇文章主要介紹了Vue如何根據(jù)角色獲取菜單動態(tài)添加路由,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • vue 移動端適配方案詳解

    vue 移動端適配方案詳解

    這篇文章主要介紹了vue 移動端適配方案詳解,詳細(xì)的介紹2種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • Vue.js實現(xiàn)簡單計時器應(yīng)用

    Vue.js實現(xiàn)簡單計時器應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了Vue.js實現(xiàn)簡單計時器應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 前端構(gòu)建工具Webpack、Vite區(qū)別有哪些

    前端構(gòu)建工具Webpack、Vite區(qū)別有哪些

    Webpack和Vite是兩種主流的前端構(gòu)建工具,它們在功能、性能和使用場景上有所不同,Webpack提供豐富的功能和配置,適合大型復(fù)雜項目,但可能導(dǎo)致啟動和構(gòu)建速度較慢,Vite基于ES模塊,支持快速的熱替換,適合小型或中等項目,需要的朋友可以參考下
    2024-10-10
  • vue實現(xiàn)簡單的跑馬燈效果

    vue實現(xiàn)簡單的跑馬燈效果

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)簡單的跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Vue3+ElementPlus在el-table表格中顯示時間的示例代碼

    Vue3+ElementPlus在el-table表格中顯示時間的示例代碼

    文章介紹了如何在Vue3+ElementPlus的el-table表格中顯示時間,并提供了相關(guān)的代碼示例,感興趣的朋友一起看看吧
    2025-02-02
  • vue?openlayers實現(xiàn)臺風(fēng)軌跡示例詳解

    vue?openlayers實現(xiàn)臺風(fēng)軌跡示例詳解

    這篇文章主要為大家介紹了vue?openlayers實現(xiàn)臺風(fēng)軌跡示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論

广丰县| 繁峙县| 油尖旺区| 潞西市| 登封市| 积石山| 灌阳县| 沙河市| 闽清县| 稻城县| 万宁市| 江安县| 会理县| 玉龙| 鄢陵县| 紫阳县| 阜新| 改则县| 中卫市| 云龙县| 楚雄市| 中超| 吉木乃县| 宜君县| 大竹县| 唐河县| 开平市| 广饶县| 陈巴尔虎旗| 金沙县| 祁东县| 金溪县| 宝丰县| 咸丰县| 长泰县| 中西区| 济源市| 彩票| 汉寿县| 庄河市| 宁乡县|