V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yantianqi
V2EX  ›  程序员

vue 中引 css 一直找不到位置

  •  
  •   yantianqi · 2017-12-06 14:29:44 +08:00 · 4790 次点击
    这是一个创建于 2323 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 vue init webpack blog 来初始化的项目
    在 App.vue 中的

    <style>
    @import '~static/style.css';
    </style>
    

    在根目录下的 static 有 style.css
    在 webpack 里面也配置了 alias 了

    alias: {
          'static': resolve('static'),
          '@': resolve('src')
        }
    

    怎么找不到文件呢?

    9 条回复    2017-12-07 09:57:17 +08:00
    shengchao
        1
    shengchao  
       2017-12-06 14:40:02 +08:00
    看我的 alias 里是
    ```
    alias: {
    'vue$': 'vue/dist/vue.esm.js',
    '@': resolve('src')
    }
    ```
    引用就是
    ```
    import Hello from '@/components/Hello'
    ```
    airyland
        2
    airyland  
       2017-12-06 14:52:59 +08:00   ❤️ 1
    一个问题何必发 3 个帖子,况且都已经有问题,这不是好好提问题了,是 spam。 @livid
    sensui7
        3
    sensui7  
       2017-12-06 15:09:12 +08:00
    ~一般是 node_module 目录, 你确定要用~开头吗? 欢迎使用我的脚本,https://www.v2ex.com/t/411666
    yantianqi
        4
    yantianqi  
    OP
       2017-12-06 15:36:12 +08:00
    @sensui7 引用静态资源,如果不用~的话
    很多情况下要../../../
    sensui7
        5
    sensui7  
       2017-12-06 16:37:47 +08:00
    @yantianqi 我不知道你的意思, 完全可以自己定义一个目录在 resolve 里,
    meszyouh
        6
    meszyouh  
       2017-12-06 16:40:25 +08:00
    直接这样
    // root = path.resolve(".");
    {
    “@”:path.join(root, "src")
    }
    yantianqi
        7
    yantianqi  
    OP
       2017-12-06 16:59:58 +08:00
    @sensui7 @meszyouh 我指的是在 vue 文件中,style 里面导入 css
    ```css
    <style>
    @import '~static/style.css'
    </style>
    ```
    或者在 vue 的页面中引入图片
    ```html
    <img src="~static/img/xxx.png">
    ```
    kuoruan
        8
    kuoruan  
       2017-12-06 18:53:50 +08:00
    我和你一样做了 alias,但是我 alias 的是 @assets
    ```
    @import "~@assets/css/style.css"
    ```
    这是可以正常获取到的
    misaka20
        9
    misaka20  
       2017-12-07 09:57:17 +08:00
    一般放在 static 文件中的 CSS,直接在 index.html 引入
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1133 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:56 · PVG 06:56 · LAX 15:56 · JFK 18:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.