Neutrinojs / vue项目:从根文件夹导入vue文件/模板

时间:2018-08-19 09:55:09

标签: node.js webpack vue.js import neutrino

我想从根文件夹开始导入,而不是向下几个文件夹。 我该怎么办?

在尝试为我的主页导入留言簿(如1999年)脚本时遇到了这个问题。

import MyGuestbook from '../../components/MyGuestbook.vue'编译。

import MyGuestbook from '/src/components/MyGuestbook.vue'没有。

import MyGuestbook from '@/components/MyGuestbook.vue'也不是。

GuestBookPage.vue

<template>
<div>
    <MyGuestbook />
</div>
</template>

<script>
import MyGuestbook from '../../components/MyGuestbook.vue'

export default {
  name: 'GuestbookPage',
  components: {
    MyGuestbook
  }
};
</script>

MyGuestbook.vue

<template>
  </div>
  </div>
</template>

<script src="non-editable-comments-v0.1.0.js"></script>
<script>
  window.safeComments('CommentTitle', 'CommentTargetID');

  export default {
    name: 'MyGuestbook'
  };
</script>

[更新]

我知道我需要使用webpack,但是我的项目将neutrinojs / vue(建议用于项目类型)与webpack结合使用。我对它们都不熟悉。到目前为止,添加 webpack.config.js 无效。

webpack.config.js

module.exports = {
  resolve: {
    extensions: [
      '.js', '.vue', '.json'
    ],
    alias: {
      '@': resolve('src'),
      '@root': resolve('.')
    }
  }
};

Nor正在编辑 .neutrinorc.js

module.exports = {
  use: [
    '@neutrinojs/standardjs',
    [
      '@neutrinojs/vue',
      {
        html: {
          title: 'MySite'
        }
      }
    ],
    '@neutrinojs/jest',
    (neutrino) => {
      neutrino.config.resolve.extensions
        .add('.js')
        .add('.json')
        .add('.vue');

      neutrino.config.resolve.alias
      .set('@', 'src')
      .set('@root', '.');
    }
  ]
};

错误:

ERROR in ./node_modules/@neutrinojs/compile-loader/node_modules/babel-loader/lib?{"cacheDirectory":true,
"plugins":[["/home/folatt/Cloud/workspace/atom/Sites/MySite/node_modules/fast-async/plugin.js",
{"spec":true}],
"/home/folatt/Cloud/workspace/atom/Sites/MySite/node_modules/babel-plugin-syntax-dynamic-import/lib/index.js",
"/home/folatt/Cloud/workspace/atom/Sites/MySite/node_modules/babel-plugin-transform-object-rest-spread/lib/index.js"],
"presets":[["/home/folatt/Cloud/workspace/atom/Sites/MySite/node_modules/babel-preset-env/lib/index.js",
{"debug":false,"modules":false,
"useBuiltIns":true,"exclude":["transform-regenerator",
"transform-async-to-generator"],
"targets":{"browsers":[
"last 2 Chrome versions",
"last 2 Firefox versions",
"last 2 Edge versions",
"last 2 Opera versions",
"last 2 Safari versions",
"last 2 iOS versions"]}}]]}!.
/node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/docs/7.forum/GuestbookPage.vue
Module not found: Error: Can't resolve '@/components/YroGuestbook.vue' in '/home/folatt/Cloud/workspace/atom/Sites/MySite/src/docs/7.forum'
 @ ./node_modules/@neutrinojs/compile-loader/node_modules/babel-loader/lib?{"cacheDirectory":true,
"plugins":[["/home/folatt/Cloud/workspace/atom/Sites/MySite/node_modules/fast-async/plugin.js",
{"spec":true}],
"/home/folatt/Cloud/workspace/atom/Sites/MySite/node_modules/babel-plugin-syntax-dynamic-import/lib/index.js",
"/home/folatt/Cloud/workspace/atom/Sites/MySite/node_modules/babel-plugin-transform-object-rest-spread/lib/index.js"],
"presets":[["/home/folatt/Cloud/workspace/atom/Sites/MySite/node_modules/babel-preset-env/lib/index.js",
{"debug":false,"modules":false,"useBuiltIns":true,
"exclude":["transform-regenerator","transform-async-to-generator"],
"targets":{"browsers":["last 2 Chrome versions",
"last 2 Firefox versions",
"last 2 Edge versions",
"last 2 Opera versions",
"last 2 Safari versions",
"last 2 iOS versions"]}}]]}!.
/node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/docs/7.forum/GuestbookPage.vue 8:0-57
 @ ./src/docs/7.forum/GuestbookPage.vue
 @ ./src/router/index.js
 @ ./src/index.js
 @ multi ./node_modules/webpack-dev-server/client?http://localhost:5000 ./node_modules/@neutrinojs/web/node_modules/webpack/hot/dev-server.js ./src/index

1 个答案:

答案 0 :(得分:4)

我错过了整个babel插件。

yarn add babel-plugin-root-import -dev

.babelrc

{
  "plugins": [
    ["babel-plugin-root-import", {
      "rootPathSuffix": "src",
      "rootPathPrefix": "@"
    }]
  ]
}

我无法使别名解析正常工作,但是确实似乎可行。