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

vue-cli3 項(xiàng)目?jī)?yōu)化之通過(guò) node 自動(dòng)生成組件模板 generate View、Component

 更新時(shí)間:2019年04月30日 14:43:15   作者:多多ing  
這篇文章主要介紹了vue-cli3 項(xiàng)目?jī)?yōu)化之通過(guò) node 自動(dòng)生成組件模板 generate View、Component的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

介紹

做前端的大家都知道通過(guò) vue 開(kāi)發(fā)的項(xiàng)目每次創(chuàng)建新組建的時(shí)候,都要新建一個(gè)目錄,然后新增 .vue 文件,在這個(gè)文件中再寫(xiě)入 template 、 script 、 style 這些內(nèi)容,雖然在寫(xiě)入的時(shí)候大家都有自己的自動(dòng)補(bǔ)全共計(jì),不過(guò)這些都是模板性的,每次都要這樣重復(fù)操作,很麻煩有沒(méi)有。

本文就是通過(guò)node來(lái)幫助我們,自動(dòng)去執(zhí)行這些重復(fù)操作,而我們只需要告訴控制臺(tái)我們需要?jiǎng)?chuàng)建的組件名字就可以了。
本文自動(dòng)創(chuàng)建的組件包含兩個(gè)文件:入口文件 index.js 、vue文件 main.vue

chalk工具

為了方便我們能看清控制臺(tái)輸出的各種語(yǔ)句,我們先安裝一下 chalk

npm install chalk --save-dev

1. 創(chuàng)建views

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

  • 在 scripts 中創(chuàng)建 generateView 文件夾
  • 在 generateView 中新建 index.js ,放置生成組件的代碼
  • 在 generateView 中新建 template.js ,放置組件模板的代碼,模板內(nèi)容可根據(jù)項(xiàng)目需求自行修改

index.js

// index.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}`))
// 導(dǎo)入模板
const {
  vueTemplate,
  entryTemplate
} = require('./template')
// 生成文件
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)
      }
    })
  })
}
log('請(qǐng)輸入要生成的頁(yè)面組件名稱、會(huì)生成在 views/目錄下')
let componentName = ''
process.stdin.on('data', async chunk => {
  // 組件名稱
  const inputName = String(chunk).trim().toString()
  // Vue頁(yè)面組件路徑
  const componentPath = resolve('../../src/views', inputName)
  // vue文件
  const vueFile = resolve(componentPath, 'main.vue')
  // 入口文件
  const entryFile = resolve(componentPath, 'entry.js')
  // 判斷組件文件夾是否存在
  const hasComponentExists = fs.existsSync(componentPath)
  if (hasComponentExists) {
    errorLog(`${inputName}頁(yè)面組件已存在,請(qǐng)重新輸入`)
    return
  } else {
    log(`正在生成 component 目錄 ${componentPath}`)
    await dotExistDirectoryCreate(componentPath)
  }
  try {
    // 獲取組件名
    if (inputName.includes('/')) {
      const inputArr = inputName.split('/')
      componentName = inputArr[inputArr.length - 1]
    } else {
      componentName = inputName
    }
    log(`正在生成 vue 文件 ${vueFile}`)
    await generateFile(vueFile, vueTemplate(componentName))
    log(`正在生成 entry 文件 ${entryFile}`)
    await generateFile(entryFile, entryTemplate(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()
    })
  }
}

template.js

// template.js
module.exports = {
  vueTemplate: compoenntName => {
    return `<template>
 <div class="${compoenntName}">
 ${compoenntName}組件
 </div>
</template>
<script>
export default {
 name: '${compoenntName}'
};
</script>
<style lang="stylus" scoped>
.${compoenntName} {
};
</style>`
  },
  entryTemplate: compoenntName => {
    return `import ${compoenntName} from './main.vue'
export default [{
 path: "/${compoenntName}",
 name: "${compoenntName}",
 component: ${compoenntName}
}]`
  }
}

1.1 配置package.json

"new:view": "node ./scripts/generateView/index"

如果使用 npm 的話 就是 npm run new:view
如果是 yarn 自行修改命令

1.2 結(jié)果

2. 創(chuàng)建component

跟views基本一樣的步驟

  • 在 scripts 中創(chuàng)建 generateComponent 文件夾
  • 在 generateComponent 中新建 index.js ,放置生成組件的代碼
  • 在 generateComponent 中新建 template.js ,放置組件模板的代碼,模板內(nèi)容可根據(jù)項(xiàng)目需求自行修改

index.js

// index.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 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)
      }
    })
  })
}
log('請(qǐng)輸入要生成的組件名稱、如需生成全局組件,請(qǐng)加 global/ 前綴')
let componentName = ''
process.stdin.on('data', async chunk => {
  const inputName = String(chunk).trim().toString()
    /**
     * 組件目錄路徑
     */
  const componentDirectory = resolve('../../src/components', inputName)
  /**
   * vue組件路徑
   */
  const componentVueName = resolve(componentDirectory, 'main.vue')
    /**
     * 入口文件路徑
     */
  const entryComponentName = resolve(componentDirectory, 'index.js')
  const hasComponentDirectory = fs.existsSync(componentDirectory)
  if (hasComponentDirectory) {
    errorLog(`${inputName}組件目錄已存在,請(qǐng)重新輸入`)
    return
  } else {
    log(`正在生成 component 目錄 ${componentDirectory}`)
    await dotExistDirectoryCreate(componentDirectory)
      // fs.mkdirSync(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)
  }
  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()
    })
  }
}

template.js

// template.js
module.exports = {
  vueTemplate: compoenntName => {
    return `<template>
 <div class="${compoenntName}">
 ${compoenntName}組件
 </div>
</template>
<script>
export default {
 name: '${compoenntName}'
};
</script>
<style lang="stylus" scoped>
.${compoenntName} {
};
</style>`
  },
  entryTemplate: `import Main from './main.vue'
export default Main`
}

2.1 配置package.json

"new:comp": "node ./scripts/generateComponent/index"

  • 如果使用 npm 的話 就是 npm run new:comp
  • 如果是 yarn 自行修改命令

2.2 結(jié)果

通過(guò)以上的 vue-cli3 優(yōu)化,我們項(xiàng)目在開(kāi)發(fā)的過(guò)程中就能非常方便的通過(guò)命令快速創(chuàng)建公共組件和其他頁(yè)面了,在頁(yè)面、組件比較多的項(xiàng)目中,可以為我們提高一些效率,也可以通過(guò)這樣的命令,來(lái)控制團(tuán)隊(duì)內(nèi)不同人員新建文件的格式規(guī)范。

總結(jié)

以上所述是小編給大家介紹的vue-cli3 項(xiàng)目?jī)?yōu)化之通過(guò) node 自動(dòng)生成組件模板 generate View、Component,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

  • vue3?reactive數(shù)據(jù)更新視圖不更新問(wèn)題解決

    vue3?reactive數(shù)據(jù)更新視圖不更新問(wèn)題解決

    這篇文章主要為大家介紹了vue3?reactive數(shù)據(jù)更新視圖不更新問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Vue.js中computed的基本使用方法

    Vue.js中computed的基本使用方法

    Vue.js中,computed屬性根據(jù)依賴進(jìn)行緩存,只有依賴改變時(shí)才重新計(jì)算,這樣有效提高性能,computed屬性是響應(yīng)式的,可以自動(dòng)更新,并且默認(rèn)是只讀的,它與methods的主要區(qū)別在于計(jì)算屬性具有緩存性,而方法每次調(diào)用都會(huì)執(zhí)行,使用computed可以使模板更加簡(jiǎn)潔,提高應(yīng)用性能
    2024-09-09
  • 前端儲(chǔ)存之localStrage、sessionStrage和Vuex使用

    前端儲(chǔ)存之localStrage、sessionStrage和Vuex使用

    localStorage、sessionStorage和Vuex是三種不同的客戶端存儲(chǔ)方式,用于在瀏覽器中保存數(shù)據(jù),localStorage和sessionStorage都是以鍵值對(duì)的形式存儲(chǔ)數(shù)據(jù),但localStorage存儲(chǔ)的數(shù)據(jù)在關(guān)閉瀏覽器后仍然存在
    2025-01-01
  • UniApp中實(shí)現(xiàn)類似錨點(diǎn)定位滾動(dòng)效果

    UniApp中實(shí)現(xiàn)類似錨點(diǎn)定位滾動(dòng)效果

    一個(gè)uniapp小程序的項(xiàng)目,我們需要實(shí)現(xiàn)一個(gè)非常實(shí)用的功能——類似于錨點(diǎn)定位的交互效果,即在首頁(yè)中有多個(gè)tab(分類標(biāo)簽),每個(gè)tab對(duì)應(yīng)著不同的模塊,當(dāng)用戶點(diǎn)擊某個(gè)分類的tab時(shí),需要流暢地滾動(dòng)到對(duì)應(yīng)的內(nèi)容位置,提供更好的用戶體驗(yàn),
    2023-10-10
  • 關(guān)于vue組件的更新機(jī)制?resize()?callResize()

    關(guān)于vue組件的更新機(jī)制?resize()?callResize()

    這篇文章主要介紹了關(guān)于vue組件的更新機(jī)制?resize()?callResize(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 使用vue3+TS實(shí)現(xiàn)簡(jiǎn)易組件庫(kù)的全過(guò)程

    使用vue3+TS實(shí)現(xiàn)簡(jiǎn)易組件庫(kù)的全過(guò)程

    當(dāng)市面上主流的組件庫(kù)不能滿足我們業(yè)務(wù)需求的時(shí)候,那么我們就有必要開(kāi)發(fā)一套屬于自己團(tuán)隊(duì)的組件庫(kù),下面這篇文章主要給大家介紹了如何使用vue3+TS實(shí)現(xiàn)簡(jiǎn)易組件庫(kù)的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • vue3項(xiàng)目啟動(dòng)自動(dòng)打開(kāi)瀏覽器以及server配置過(guò)程

    vue3項(xiàng)目啟動(dòng)自動(dòng)打開(kāi)瀏覽器以及server配置過(guò)程

    這篇文章主要介紹了vue3項(xiàng)目啟動(dòng)自動(dòng)打開(kāi)瀏覽器以及server配置過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue子組件通過(guò).sync修飾符修改props屬性方式

    vue子組件通過(guò).sync修飾符修改props屬性方式

    這篇文章主要介紹了vue子組件通過(guò).sync修飾符修改props屬性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • ElementUI下拉框選擇后不顯示值問(wèn)題及解決

    ElementUI下拉框選擇后不顯示值問(wèn)題及解決

    這篇文章主要介紹了ElementUI下拉框選擇后不顯示值問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • element表格組件實(shí)現(xiàn)右鍵菜單的功能

    element表格組件實(shí)現(xiàn)右鍵菜單的功能

    本文主要介紹了element表格組件實(shí)現(xiàn)右鍵菜單的功能,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論

富平县| 泊头市| 洛浦县| 大关县| 措美县| 铜梁县| 荆门市| 连平县| 囊谦县| 沁源县| 枞阳县| 邹城市| 临桂县| 新民市| 崇信县| 阿瓦提县| 南涧| 建平县| 托克托县| 东山县| 濉溪县| 新建县| 浑源县| 洱源县| 蓝山县| 长子县| 红安县| 枝江市| 鲜城| 松溪县| 鹤庆县| 兴城市| 襄城县| 波密县| 盱眙县| 石景山区| 甘洛县| 怀来县| 景德镇市| 越西县| 格尔木市|