V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
yantianqi
V2EX  ›  程序员

vue 中引 css 一直找不到位置

  •  
  •   yantianqi · Dec 6, 2017 · 5259 views
    This topic created in 3076 days ago, the information mentioned may be changed or developed.

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

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

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

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

    怎么找不到文件呢?

    9 replies    2017-12-07 09:57:17 +08:00
    shengchao
        1
    shengchao  
       Dec 6, 2017
    看我的 alias 里是
    ```
    alias: {
    'vue$': 'vue/dist/vue.esm.js',
    '@': resolve('src')
    }
    ```
    引用就是
    ```
    import Hello from '@/components/Hello'
    ```
    airyland
        2
    airyland  
       Dec 6, 2017   ❤️ 1
    一个问题何必发 3 个帖子,况且都已经有问题,这不是好好提问题了,是 spam。 @livid
    sensui7
        3
    sensui7  
       Dec 6, 2017
    ~一般是 node_module 目录, 你确定要用~开头吗? 欢迎使用我的脚本,https://www.v2ex.com/t/411666
    yantianqi
        4
    yantianqi  
    OP
       Dec 6, 2017
    @sensui7 引用静态资源,如果不用~的话
    很多情况下要../../../
    sensui7
        5
    sensui7  
       Dec 6, 2017
    @yantianqi 我不知道你的意思, 完全可以自己定义一个目录在 resolve 里,
    meszyouh
        6
    meszyouh  
       Dec 6, 2017
    直接这样
    // root = path.resolve(".");
    {
    “@”:path.join(root, "src")
    }
    yantianqi
        7
    yantianqi  
    OP
       Dec 6, 2017
    @sensui7 @meszyouh 我指的是在 vue 文件中,style 里面导入 css
    ```css
    <style>
    @import '~static/style.css'
    </style>
    ```
    或者在 vue 的页面中引入图片
    ```html
    <img src="~static/img/xxx.png">
    ```
    kuoruan
        8
    kuoruan  
       Dec 6, 2017
    我和你一样做了 alias,但是我 alias 的是 @assets
    ```
    @import "~@assets/css/style.css"
    ```
    这是可以正常获取到的
    misaka20
        9
    misaka20  
       Dec 7, 2017
    一般放在 static 文件中的 CSS,直接在 index.html 引入
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1371 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 17:23 · PVG 01:23 · LAX 10:23 · JFK 13:23
    ♥ Do have faith in what you're doing.