Home > Web Front-end > Vue.js > body text

Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing

青灯夜游
Release: 2022-11-04 20:11:44
forward
3559 people have browsed it

Why use require when dynamically introducing images into

vue? The following article will talk to you about the things you don’t know. I hope it will be helpful to you!

Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing

I believe that friends who have used vue must have been asked this question by the interviewer: Why use require when dynamically introducing images in vue . [Related recommendations: vuejs video tutorial, web front-end development]

Some friends may smile scornfully: Oh, that’s it, because of dynamic addition src is treated as a static resource and is not compiled, so require must be added. I can recite it backwards...

emmm... At first glance, it seems like that It makes sense, but if you look carefully, what exactly does this sentence mean? In response to the above answer, I can't help but have the following questions:

  • What are static resources?

  • Why is the dynamically added src regarded as a static resource?

  • If it is not compiled, why is it not compiled?

  • Why can resources be imported correctly by adding require? Is it because it can be compiled by adding require?

When I had the last question, I found that the answer above seemed to say something, but it didn’t seem to say anything... If you read the answer I also have a few questions like the above, so let me answer them one by one

1. What are static resources

and static resources There is also a corresponding dynamic resource. Let us first see how the big guys on the Internet explain it.

Static resources: Generally, the client sends a request to the web server. The web server retrieves the corresponding file from the memory and returns it to the client. The client parses, renders and displays it.

Dynamic resources: Generally, for dynamic resources requested by the client, the request is first handed over to the web container, and the web container connects to the database. After the database processes the data, the content is handed over to the web server, and the web server returns it to the client for parsing and rendering. deal with.

In fact, the above summary is very clear. From the perspective of a vue project, we can simply understand it as:

Static resources are resources stored directly in the project. These resources do not require us to send special requests to obtain them. For example, pictures, videos, audios, font files, css style sheets, etc. under the assets directory.

Dynamic resources are resources that need to be obtained by sending a request. For example, when we browse Taobao, different product information is obtained by sending special requests, which can be called dynamic resources.

2. Why is the dynamically added src regarded as a static resource?

Before answering this question, we need to understand how the browser can run a vue project.

We know that when a browser opens a web page, it actually runs three types of files: html, css, and js. When we start a vue project locally, we actually package the vue project first. The packaging process is the process of transpiling each vue file in the project into html, css, and js files, and then running them on the browser In progress.

If we do not use require to introduce the dynamically added src, what will it look like in the end? I will take you to experiment.

// vue文件中动态引入一张图片
<template>
  <div class="home">
      <!-- 通过v-bind引入资源的方式就称之为动态添加 -->
    <img :src="&#39;../assets/logo.png&#39;" alt="logo">
  </div>
</template>

//最终编译的结果(浏览器上运行的结果)
//这张图片是无法被正确打开的
<img src="../assets/logo.png" alt="logo">
Copy after login

We can see that the dynamically added src will eventually be compiled into a static string address. When the program is running, it will import resources into the project directory according to this address. The way to introduce resources into the project directory is to treat the resources as static resources. So this also answers our question 2.

After seeing this, some friends may be confused. Is there anything wrong with this finally compiled address? The pictures in my project are at this address, why can't they be imported? Don't worry, let's continue reading.

3. If it is not compiled, what does it mean that it is not compiled?

Not compiled. This half-sentence is very confusing. According to question 2, we know that the dynamically imported image is eventually compiled, but the image resource cannot be imported correctly after being compiled. So this sentence is inherently wrong. Regarding our standard answer, I will rewrite it here:

Because dynamically added src is treated as a static resource, and the compiled static path cannot correctly introduce resources, so it must be added require

Then a new question arises here: Why can’t the compiled static path correctly introduce resources?

想得到这个问题的答案,我们得先从正常的引入一张图片开始。在项目中我们静态的引入一张图片肯定是可以引入成功的,而引用图片所在的vue文件肯定也是被编译的,那静态引入图片最终会被编译成什么样呢,模拟一波:

// vue文件中静态的引入一张图片
<template>
  <div class="home">
      <!-- 直接引入图片静态地址, 不再使用v-bind -->
    <img src="../assets/logo.png" alt="logo">
  </div>
</template>

//最终编译的结果
//这张图片是可以被正确打开的
<img src="/img/logo.6c137b82.png" alt="logo">
Copy after login

根据上面的测试,我们发现,使用静态的地址去引入一张图片,图片的路径和图片的名称已经发生了改变,并且编译后过后的静态地址是可以成功的引入资源的。这是因为,在默认情况下,src目录下面的所有文件都会被打包,src下面的图片也会被打包在新的文件夹下并生成新的文件名。编译过后的静态地址引入的是打包过后的图片地址,从而可以正确的引用资源

事实确实是这样吗?我们可以执行打包命令(npm run build)进行验证

Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing

可以发现,编译过后的静态地址确实是和dist下编译后图片地址是一致的,从而验证我们的想法。

到这里我们其实就可以解释上面的问题了:动态添加的src,被编译过后的静态路径为什么无法正确的引入资源?

因为动态的添加的src编译过后的地址,与图片资源编译过后的资源地址不一致, 导致无法正确的引入资源

  编译过后的src地址:../assets/logo.png
  编译过后的图片资源地址:/img/logo.6c137b82.png
Copy after login

那要怎么解决上述的问题呢,答案就是:require

4. 加上require为什么能正确的引入资源,是因为加上require就能编译了?

针对这个问题,首先就要否定后半句,无论加不加require,vue文件中引入一张图片都会被编译。

接着我们再来好好了解一下,require。

4.1 require是什么: 是一个node方法,用于引入模块,JSON或本地文件

4.2 调用require方法引入一张图片之后发生了什么:

在回答这个问题之前,容我先对问题3中的内容进行一定的补充。其实如果真的有小伙伴跟着问题三中的操作进行验证,估计就要开喷了:为什么我静态引入的图片最终编译的地址和你的不一样,是个base64,而且打包之后dist下面也没有生成新的图片。大概就是下面这样的情况。

// vue文件中静态的引入一张图片
<template>
<div class="home">
    <!-- 直接引入图片静态地址, 不再使用v-bind -->
  <img src="../assets/logo.png" alt="logo">
</div>
</template>

//最终编译的结果
//这张图片是可以被正确打开的
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyNpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDE0IDc5LjE1Njc5NywgMjAxNC8wOC8yMC0wOTo1MzowMiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIg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AAAAElFTkSuQmCC" alt="logo">
Copy after login

先别急着喷,实际上造成这种差异的原因,是因为我改了一下webpack中的配置。接下来涉及少量webpack代码,不了解webpack的小伙伴也没关系,了解原理即可。

在上文中的我们提到,vue项目最终会被打包成一个dist目录,那么是什么帮我们完成这个打包的呢,没错,就是webpack。在vue项目中的引入一张图片的时候,细心的同学会发现,有的时候,浏览器上显示图片地址是一个base64,有的时候,是一个被编译过后的文件地址。也就是上述描述的差异。

之所以会造成这种差异,是webpack打包的时候,对图片资源进行了相关的配置。我们可以通过如下命令生成vue项目中的webpack配置文件,进行验证:

npx vue-cli-service inspect --mode development >> webpack.config.development.js

Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing

上图就是vue中webpack默认的图片打包规则。设置 type: 'asset',默认的,对于小于8k的图片,会将图片转成base64 直接插入图片,不会再在dist目录生成新图片。对于大于8k的图片,会打包进dist目录,之后将新图片地址返回给src。

而我在上述测试中使用的图片,是vue-cli自带的一张logo图片,大小是6.69k。按照默认的打包规则,是会转成base64,嵌入图片中的。所以为了讲述方便,我在vue.config.js中修改了其默认的配置,配置如下:

module.exports = {
    // 使用configureWebpack对象,下面可以直接按照webpack中的写法进行编写
    // 编写的内容,最终会被webpack-merge插件合并到webpack.config.js主配置文件中
  configureWebpack: { 
    module: {
      rules: [
        {
          test: /\.(png|jpe?g|gif|webp|avif)(\?.*)?$/,
          type: &#39;asset&#39;,
          parser: {
            dataUrlCondition: {
             // 这里我将默认的大小限制改成6k。
              // 当图片小于6k时候,使用base64引入图片;大于6k时,打包到dist目录下再进行引入
              maxSize: 1024 * 6
            }
          }
        }
      ]
    }
  }
}
Copy after login

那上面说了这么多,和require有啥关系,自然是有滴。

我们现在知道vue最终是通过webpack打包,并且会在webpack配置文件中编写一系列打包规则。而webpack中的打包规则,针对的其实是一个一个模块,换而言之webpack只会对模块进行打包。那webpack怎么将图片当成一个模块呢,这就要用到我们的正主require。

当我们使用require方法引入一张图片的时候,webpack会将这张图片当成一个模块,并根据配置文件中的规则进行打包。我们可以将require当成一个桥梁,使用了require方法引入的资源,该资源就会当成模块并根据配置文件进行打包,并返回最终的打包结果。

回到问题4.2:调用require方法引入一张图片之后发生了什么

1.如果这张图片小于项目中设置的资源限制大小,则会返回图片的base64插入到require方法的调用处

2.如果这张图片大于项目中设置的资源限制大小,则会将这个图片编译成一个新的图片资源。require方法返回新的图片资源路径及文件名

回到问题4:为什么加上require能正确的引入资源

因为通过require方法拿到的文件地址,是资源文件编译过后的文件地址(dist下生成的文件或base64文件),因此可以找对应的文件,从而成功引入资源。

答案就是这么简单,来验证一波

// vue文件中使用require动态的引入一张图片
<template>
  <div class="home">
      <!-- 使用require动态引入图片 -->
      <img :src="require(&#39;../assets/logo.png&#39;)" alt="logo">
  </div>
</template>

//最终编译的结果
//这张图片是可以被正确打开的
<img src="/img/logo.6c137b82.png" alt="logo">
Copy after login

Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing

有问题吗,没有问题。到这里,不妨再对我们的标准答案进行一次优化:

因为动态添加的src,编译过后的文件地址和被编译过后的资源文件地址不一致,从而无法正确引入资源。而使用require,返回的就是资源文件被编译后的文件地址,从而可以正确的引入资源

看到这,估计还是有一些小伙伴有一些疑问,我再扩展一波:

6. 问题3中,静态的引入一张图片,没有使用require,为什么返回的依然是编译过后的文件地址?

答:在webpack编译的vue文件的时候,遇见src等属性会默认的使用require引入资源路径。引用vue-cli官方的一段原话

当你在 JavaScript、CSS 或 *.vue 文件中使用相对路径 (必须以 . 开头) 引用一个静态资源时,该资源将会被包含进入 webpack 的依赖图中。在其编译过程中,所有诸如 <img src="..." alt="Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing" >background: url(...) 和 CSS @import 的资源 URL 都会被解析为一个模块依赖

例如,url(./Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing) 会被翻译为 require(&#39;./Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing&#39;),而:

<img  src="./Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing" alt="Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing" >
Copy after login

将会被编译到:

h(&#39;img&#39;, { attrs: { src: require(&#39;./Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing&#39;) }})
Copy after login

7. 按照问题6中所说,那么动态添加src的时候也会使用require引入,为什么src编译过后的地址,与图片资源编译过后的资源地址不一致

答:因为动态引入一张图片的时候,src后面的属性值,实际上是一个变量。webpack会根据v-bind指令去解析src后面的属性值。并不会通过reuqire引入资源路径。这也是为什么需要手动的添加require。

8.据说public下面的文件不会被编译,那我们使用静态路径去引入资源的时候,也会默认的使用require引入吗?

官方的原文是这样子的:

任何放置在 public 文件夹的静态资源都会被简单的复制,而不经过 webpack。你需要通过绝对路径来引用它们。

答:不会,使用require引入资源的前提的该资源是webpack解析的模块,而public下的文件压根就不会走编译,也就不会使用到require。

9.为什么使用public下的资源一定要绝对路径

答:因为虽然public文件不会被编译,但是src下的文件都会被编译。由于引入的是public下的资源,不会走require,会直接返回代码中的定义的文件地址,该地址无法在编译后的文件目录(dist目录)下找到对应的文件,会导致引入资源失败。

10.上文件中提到的webpack,为什么引入资源的时候要有base64和打包到dist目录下两种的方式,全部打包到的dist目录下,他不香吗?

答:为了减少http请求。页面中通过路径引入的图片,实际上都会向服务器发送一个请求拿到这张图片。对于资源较小的文件,设置成base64,既可以减少请求,也不会影响到页面的加载性能。

(学习视频分享:编程基础视频

The above is the detailed content of Why do we need to use require to dynamically introduce images in Vue? Analysis and sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template