“ @ / filename”语法在vue-cli项目

时间:2018-12-18 01:40:17

标签: javascript typescript vue.js es6-modules vue-cli-3

我使用vue-cli(版本3)创建了一个项目。该项目允许您在@语句中使用别名import来引用src目录。像这样:

src / components / ArticleList.vue

import articles from '@/articleIndex.ts';

将成功导入 src / articleIndex.ts

但是, src / views / Article.vue 中的相同声明不起作用:

import articles from '@/articleIndex.ts';

这将导致错误“导入语句不能以.ts结尾。请考虑导入@/articleIndex。”所以,我尝试了。

src / views / Article.vue

import articleList from '@/articleIndex';

这将导致错误“找不到模块:'@ / articleIndex'。

articleIndexarticleIndex.ts都在 src / components / ArticleList.vue 中工作。

但是,以下方法确实有效:

src / views / Article.vue

import articles from '../articleIndex';

为什么使用@时一个文件在import语句周围引发错误,而另一个文件不是

下面是我的package.json。这是vue-cli似乎存储所有配置选项的地方。

package.json

{
  "name": "blog",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "test:unit": "vue-cli-service test:unit"
  },
  "dependencies": {
    "register-service-worker": "^1.5.2",
    "showdown": "^1.9.0",
    "vue": "^2.5.17",
    "vue-class-component": "^6.0.0",
    "vue-property-decorator": "^7.0.0",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1"
  },
  "devDependencies": {
    "@types/jest": "^23.1.4",
    "@vue/cli-plugin-babel": "^3.0.5",
    "@vue/cli-plugin-pwa": "^3.0.5",
    "@vue/cli-plugin-typescript": "^3.0.5",
    "@vue/cli-plugin-unit-jest": "^3.0.5",
    "@vue/cli-service": "^3.0.5",
    "@vue/test-utils": "^1.0.0-beta.20",
    "babel-core": "7.0.0-bridge.0",
    "ts-jest": "^23.0.0",
    "typescript": "^3.0.0",
    "vue-template-compiler": "^2.5.17"
  },
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ],
  "jest": {
    "moduleFileExtensions": [
      "js",
      "jsx",
      "json",
      "vue",
      "ts",
      "tsx"
    ],
    "transform": {
      "^.+\\.vue$": "vue-jest",
      ".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$": "jest-transform-stub",
      "^.+\\.tsx?$": "ts-jest"
    },
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1"
    },
    "snapshotSerializers": [
      "jest-serializer-vue"
    ],
    "testMatch": [
      "**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"
    ],
    "testURL": "http://localhost/"
  }
}

0 个答案:

没有答案