vite生成vue项目使用vant组件样式问题

vite生成的vue项目中,用了vant组件库的组件,有些组件不能铺满整个空余屏幕只能固定大小,我猜测可能是因为新版本css或者组件的一些默认值改变了导致的,不知道该怎么修改呢?

img

img

这是搜索页面的代码

<template >
  <form action="/" >
    <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
    />
  form>
  <van-divider>已选标签van-divider>
  <div v-if="activeIds.length === 0">请选择标签div>
  <van-row gutter="16" style="padding: 0 16px">
    <van-col v-for="tag in activeIds">
      <van-tag closeable size="medium" type="primary" @close="close(tag)">
        {{ tag }}
      van-tag>
    van-col>
  van-row>

  <van-divider>全部标签van-divider>
  <van-tree-select
      v-model:active-id="activeIds"
      v-model:main-active-index="activeIndex"
      :items="tagList"
  />
template>
<script setup>
import {ref} from 'vue';

const searchText = ref('');
const originTagList = [
  {
    text: '性别',
    children: [
      {text: '男', id: '男'},
      {text: '女', id: '女'},
    ],
  },
  {
    text: '年级',
    children: [
      {text: '大一', id: '大一'},
      {text: '大二', id: '大二'},
      {text: '大3', id: '大3'},
      {text: '大4', id: '大4'},
      {text: '大5', id: '大5aaaaaa'},
      {text: '大6', id: '大6aaaaaa'},
    ],
  },
];
let tagList = ref(originTagList)
const onSearch = (val) => {
  tagList.value = originTagList.map(parentTag => {
    const tempChildrenTag = [...parentTag.children];
    const tempParentTag = {...parentTag};
    tempParentTag.children = tempChildrenTag.filter(item => item.text.includes(searchText.value)
    )
    return tempParentTag;
  })
};
const onCancel = () => {
  searchText.value = '';
  tagList.value = originTagList
};


const activeIds = ref([]);
const activeIndex = ref(0);
const close = (tag) => {
  activeIds.value = activeIds.value.filter(id => id !== tag)
}

script>

<style scoped>

style>


这是信息页面的代码

<template>
  <van-cell title="头像" is-link>
    <img :src="user.avatar" alt="" style="height: 46px">
  van-cell>
  <van-cell title="昵称" is-link :value="user.userName" @click="toEdit('userName', user.userName, '昵称')"/>
  <van-cell title="账号" :value="user.userAccount"/>
  <van-cell title="性别" is-link :value="user.gender" @click="toEdit('gender', user.gender, '性别')"/>
  <van-cell title="电话" is-link :value="user.phone" @click="toEdit('phone', user.phone, '电话')"/>
  <van-cell title="邮箱" is-link :value="user.email" @click="toEdit('email', user.email, '邮箱')"/>
  <van-cell title="星球编号" :value="user.planetCode"/>
  <van-cell title="注册时间" :value="user.gmtCreate"/>
template>

<script setup lang="ts">

import {ref} from "vue";
import {useRouter} from "vue-router";

const user = ref({
  id: 1,
  userName: 'ccc',
  userAccount: 'ccc',
  gender: '男',
  phone: '11',
  email: '118@qq.com',
  planetCode: '123',
  avatar: 'https://img2.baidu.com/it/u=1790834130,1952230725&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
  gmtCreate: new Date().toDateString()
})

const router = useRouter();
const toEdit = (editKey: string, currentValue: string, editName: string) => {
  router.push({
    path: '/user/edit',
    query: {
      editKey,
      currentValue,
      editName
    }
  })
}

script>

<style scoped>

style>

是不是有项目的初始css没处理,你试试原生的标签写个页面看看能铺满全屏吗

开发者工具里 审查元素 看看

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7595115
  • 这篇博客也不错, 你可以看下【vue3-vite项目按需引入Vant报错解决方法。】
  • 除此之外, 这篇博客: 解决vue3项目 vite 按需引入vant时 样式不起作用的问题中的 最后: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    暂不知道此问题是因为什么

  • 以下回答来自chatgpt:

    根据描述和代码,问题可能是因为vant组件的样式导致的显示问题,具体表现为组件无法铺满整个屏幕,只能固定一定大小。解决方案可以尝试以下几种方法:

    1. 在样式中设置宽度为100% 可以尝试在组件样式中添加宽度为100%的样式,如下:
    .your-component-class {
      width: 100%;
    }
    

    然后在组件上添加该类名。

    1. 在HTML属性中设置宽度为100% 你可以在组件上添加style属性,将宽度设置为100%,如下:
    <your-component style="width: 100%"></your-component>
    
    1. 使用内联样式 你也可以在组件上使用内联样式来设置宽度,如下:
    <your-component :style="{width: '100%'}"></your-component>
    

    如果以上方法均不起作用,则可能是组件的样式或默认值的问题,你可以查看vant组件文档或查看组件源码来获取更多信息,或者尝试在此基础上自行修改样式。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^