Vue.js 实现动态修改带icon的input样式

文章讨论了在Vue.js应用中,尝试通过JavaScript直接修改el-input组件及其子元素span的样式存在的问题,即样式在页面刷新后失效。作者提出了解决方案,即通过全局SCSS样式而非JS动态修改,以确保样式持久生效。

<el-input

            v-model.trim="loginForm.password"

            type="password"

            auto-complete="off"

            placeholder="秘钥"

            show-password

            ref ="indexInputps"

          >

            <svg-icon

              slot="prefix"

              icon-class="password"

              class="el-input__icon input-icon"

            />

          </el-input>

mounted() {

console.log("this.$refs.indexInput",this.$refs.indexInputcd.$el.querySelector('span'));

    console.log("this.$refs.indexInput.$refs",this.$refs.indexInputcd.$refs.input);

    const prefixcd = this.$refs.indexInputcd.$el.querySelector('span'); // el-input__prefix的DOM元素

    const prefixps = this.$refs.indexInputps.$el.querySelector('span');

    const prefixuc = this.$refs.indexInputuc.$el.querySelector('span');

    const innercd = this.$refs.indexInputcd.$refs.input; // el-input__inner的DOM元素

    const innerps = this.$refs.indexInputps.$refs.input; // el-input__inner的DOM元素

    const inneruc = this.$refs.indexInputuc.$refs.input; // el-input__inner的DOM元素

   //prefix.style={'backgroud':'#f0d29d !important','line-height':'48px  !important','left':'1px  !important','color':'#f0d29d !important'}; // 添加类名,修改样式

    const numh = "48px";

    const classT = "#f0d29d";

    const leftl = "1px";

    innercd.style.paddingLeft=numh;

    innercd.style.height=numh;

    innercd.style.lineHeight=numh;

    prefixcd.style.lineHeight=numh;

    prefixcd.style.background=classT;

    console.log("prefixcd.style.cssText",prefixcd.style.cssText);

    prefixcd.style.left=leftl;

    innerps.style.paddingLeft=numh;

    innerps.style.height=numh;

    innerps.style.lineHeight=numh;

    prefixps.style.lineHeight=numh;

    prefixps.style.background=classT;

    prefixps.style.left=leftl;

    inneruc.style.paddingLeft=numh;

    inneruc.style.height=numh;

    inneruc.style.lineHeight=numh;

    prefixuc.style.lineHeight=numh;

    prefixuc.style.background=classT;

    prefixuc.style.left=leftl;

}

1、这个方式有个缺陷,无法修改js生成的标签比如上面的input下的span标签样式是无效的,因为做了唯一处理,二次刷新就无效

另外一个解决方案:

<style lang="scss"> // 注意这里不要放 scoped ,不做唯一处理,其中 .login_index  form加了一层

  .login_index .el-input__prefix {

    background: #f0d29d;

    left:1px;

    line-height: 48px;

  }

  .login_index .el-input__inner {

    padding-left: 48px;

    height:48px;

    line-height: 48px;

  }

</style>

这是菜单的应用层代码,一步步分析它的作用,解释代码的意思 using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace OnMonitor.Systems { public class MenuAppService : ApplicationService, IMenuAppService { private readonly IRepository<AbpMenu, Guid> _repository; public MenuAppService(IRepository<AbpMenu, Guid> repository) { _repository = repository; } public async Task<MenuDto> Create(CreateOrUpdateMenuDto input) { var result = await _repository.InsertAsync(new AbpMenu(GuidGenerator.Create()) { FormId = input.FormId, ParentId = input.Pid, Name = input.Name, Label = input.Label, CategoryId = input.CategoryId, Sort = input.Sort, Path = input.Path, Component = input.Component, Permission = input.Permission, Icon = input.Icon, AlwaysShow = input.Pid == null ? true : false, Hidden = input.CategoryId == 2 ? true : false }); return ObjectMapper.Map<AbpMenu, MenuDto>(result); } public async Task Delete(List<Guid> ids) { //TODO:删除子集 foreach (var id in ids) { await _repository.DeleteAsync(id,true); } } public async Task<ListResultDto<MenuDto>> GetAll(GetMenuInputDto input) { var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), _ => _.Name.Contains(input.Filter)); var items =query.ToList(); var dtos = ObjectMapper.Map<List<AbpMenu>, List<MenuDto>>(items); return new ListResultDto<MenuDto>(dtos); } public async Task<MenuDto> Get(Guid id) { var result = await _repository.GetAsync(id); var dto = ObjectMapper.Map<AbpMenu, MenuDto>(result); if (dto.ParentId.HasValue) dto.Label = (await _repository.FirstOrDefaultAsync(_ => _.Id == result.ParentId))?.Label; return dto; } public async Task<ListResultDto<MenuDto>> LoadAll(Guid? id) { var items = (await _repository.GetQueryableAsync()).Where(_ => _.ParentId == id).OrderBy(_ => _.Sort).ToList(); var dtos = ObjectMapper.Map<List<AbpMenu>, List<MenuDto>>(items); return new ListResultDto<MenuDto>(dtos); } public async Task<MenuDto> Update(Guid id, CreateOrUpdateMenuDto input) { var dd = await _repository.GetListAsync(); var menu = await _repository.GetAsync(id); menu.ParentId = input.Pid; menu.CategoryId = input.CategoryId; menu.Name = input.Name; menu.Label = input.Label; menu.Sort = input.Sort; menu.Path = input.Path; menu.Component = input.Component; menu.Icon = input.Icon; menu.Permission = input.Permission; return ObjectMapper.Map<AbpMenu, MenuDto>(menu); } } }
最新发布
11-19
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慧香一格

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值