亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

不知道大家每次新建組件的時候,是不是都要創(chuàng)建一個目錄,然后新增一個.vue文件,然后寫template、script、style這些東西,如果是公共組件,是不是還要新建一個index.js用來導出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)容可以自己自定義,符合當前項目的模板即可

// 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 // 生成基礎組件
  或者
  yarn run new:view // 生成頁組件
  yarn run new:com // 生成基礎組件


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

相關文章

  • vue高德地圖繪制行政區(qū)邊界功能

    vue高德地圖繪制行政區(qū)邊界功能

    這篇文章主要介紹了vue高德地圖繪制行政區(qū)邊界功能,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2024-03-03
  • Vue自定義v-has指令,做按鈕權限判斷的步驟

    Vue自定義v-has指令,做按鈕權限判斷的步驟

    這篇文章主要介紹了Vue自定義v-has指令,做按鈕權限判斷的步驟,幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下
    2021-04-04
  • Vue實現(xiàn)簡易記事本

    Vue實現(xiàn)簡易記事本

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)簡易記事本,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue項目中Toast字體過小,沒有邊距的解決方案

    vue項目中Toast字體過小,沒有邊距的解決方案

    這篇文章主要介紹了vue項目中Toast字體過小,沒有邊距的解決方案。具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue利用插件實現(xiàn)打印功能的示例詳解

    Vue利用插件實現(xiàn)打印功能的示例詳解

    這篇文章主要為大家詳細介紹了Vue如何利用vue-print-nb插件實現(xiàn)打印功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學一下
    2023-03-03
  • Vue?子組件傳父組件?$emit更新屬性方式

    Vue?子組件傳父組件?$emit更新屬性方式

    這篇文章主要介紹了Vue?子組件傳父組件?$emit更新屬性方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 解決VUE 在IE下出現(xiàn)ReferenceError: Promise未定義的問題

    解決VUE 在IE下出現(xiàn)ReferenceError: Promise未定義的問題

    這篇文章主要介紹了解決VUE 在IE下出現(xiàn)ReferenceError: Promise未定義的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue實現(xiàn)PC端靠邊懸浮球的代碼

    Vue實現(xiàn)PC端靠邊懸浮球的代碼

    這篇文章主要介紹了Vue實現(xiàn)靠邊懸浮球(PC端)效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • vue中proxy代理的用法(解決跨域問題)

    vue中proxy代理的用法(解決跨域問題)

    這篇文章主要介紹了vue中的proxy代理的用法(解決跨域問題),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 利用vue實現(xiàn)密碼輸入框/驗證碼輸入框

    利用vue實現(xiàn)密碼輸入框/驗證碼輸入框

    這篇文章主要為大家詳細介紹了如何利用vue實現(xiàn)密碼輸入框或驗證碼輸入框的效果,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
    2023-08-08

最新評論