Several ways to preview images and videos in Vue

In Vue, there are several ways to display images

The following is a detailed code description:

1. Use <img>tags:

<template>
  <div>
    <img src="/path/to/image.jpg" alt="Image">
  </div>
</template>

In the above code, we use <img>labels to display images. srcThe attribute specifies the path of the image, and altthe attribute is used to set the alternative text of the image.

2. Use v-bindthe command to dynamically bind the image path:

<template>
  <div>
    <img v-bind:src="imageUrl" alt="Image">
  </div>
</template>

In the above code, we use v-binddirectives to dynamically bind the path of the image. imageUrlIt is a data attribute in the Vue component, which saves the path of the image.

3. Use requirethe function to load images:

<template>
  <div>
    <img :src="require('@/assets/image.jpg')" alt="Image">
  </div>
</template>

In the above code, we use requirea function to load the image. @Indicates the root directory of the project, assetswhich is the directory where pictures are stored and image.jpgthe file name of the picture.

4. Use CSS background-imageproperties:

<template>
  <div :style="{ backgroundImage: 'url(/path/to/image.jpg)' }"></div>
</template>

In the above code, we use CSS background-imageproperties to display the image. url(/path/to/image.jpg)Specifies the path to the image.

The above are several ways to display images in Vue. You can choose the appropriate way to display images based on your specific needs and projects.

In Vue, there are several ways to preview videos

Here are a few common methods:

1. Use <video>tags:

<template>
  <div>
    <video controls>
      <source :src="videoUrl" type="video/mp4">
    </video>
  </div>
</template>

In the above code, we are using <video>tags to display the video. controlsThe attribute is used to display the control bar of the video, and <source>the label specifies the path and type of the video.

2. Use a third-party video player library, such as video.js:

First, you need to install the library in your project video.js:

npm install video.js

Then, introduce and use it in the Vue component video.js:

<template>
  <div>
    <video ref="videoPlayer" class="video-js vjs-default-skin vjs-big-play-centered"></video>
  </div>
</template>

<script>
import 'video.js/dist/video-js.css';
import videojs from 'video.js';

export default {
      
      
  mounted() {
      
      
    this.initVideoPlayer();
  },
  methods: {
      
      
    initVideoPlayer() {
      
      
      this.player = videojs(this.$refs.videoPlayer, {
      
      
        controls: true,
        sources: [{
      
      
          src: this.videoUrl,
          type: 'video/mp4'
        }]
      });
    }
  }
};
</script>

In the above code, we introduced video.jsthe library and mountedinitialized the video player in the hook function of the Vue component. this.videoUrlIt is a data attribute in the Vue component, which saves the path of the video.

3. Use third-party video player components, such as vue-video-player:

First, you need to install the components in your project vue-video-player:

npm install vue-video-player

Then, introduce and use it in the Vue component vue-video-player:

<template>
  <div>
    <video-player :options="playerOptions"></video-player>
  </div>
</template>

<script>
import 'video.js/dist/video-js.css';
import 'vue-video-player/src/custom-theme.css';
import VideoPlayer from 'vue-video-player';

export default {
      
      
  components: {
      
      
    VideoPlayer
  },
  data() {
      
      
    return {
      
      
      playerOptions: {
      
      
        sources: [{
      
      
          src: this.videoUrl,
          type: 'video/mp4'
        }],
        autoplay: false,
        controls: true
      }
    };
  }
};
</script>

In the above code, we introduced vue-video-playerthe component and dataset the video player configuration items in the options of the Vue component. this.videoUrlIt is a data attribute in the Vue component, which saves the path of the video.

The above are several common methods to achieve video preview. You can choose the appropriate method to preview videos based on your specific needs and projects.

Guess you like

Origin blog.csdn.net/ACCPluzhiqi/article/details/131613973