V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
movq
V2EX  ›  程序员

vue 里面给一个组件传入绑定的值,如何把一个字符串和一个字符串变量拼接在一起?

  •  
  •   movq · 2022-11-26 11:42:42 +08:00 · 1708 次点击
    这是一个创建于 488 天前的主题,其中的信息可能已经有所发展或是发生改变。

    就比如我一个 ref 变量叫 lastModified

    我想让一个文本组件显示 "最后修改时间"+lastModified

    要怎么写才符合语法呢?

    v-bind 的时候写

    "最后修改时间"+lastModified

    或者

    "最后修改时间{{lastModified}}"

    都不行

    9 条回复    2022-11-27 00:08:15 +08:00
    sarices
        1
    sarices  
       2022-11-26 11:46:49 +08:00
    `最后修改时间{$lastModified}`
    sarices
        2
    sarices  
       2022-11-26 11:47:25 +08:00
    外面还是要加双括号的。
    hazardous
        3
    hazardous  
       2022-11-26 12:16:39 +08:00
    {{`最后修改时间${lastModified}`}}
    wdssmq
        4
    wdssmq  
       2022-11-26 12:30:49 +08:00   ❤️ 1
    虽然但是,这种情况建议使用 计算属性

    计算属性 | Vue.js
    https://cn.vuejs.org/guide/essentials/computed.html#basic-example
    Rhianu
        5
    Rhianu  
       2022-11-26 16:01:47 +08:00
    @sarices $加的位置不正确, 正确的应该是 v-text="`最后修改时间${lastModified}`" 或者 {{`最后修改时间${lastModified}`}}
    sarices
        6
    sarices  
       2022-11-26 16:11:13 +08:00
    @Rhianu php 和前端切换不过来,打错了
    hzxxx
        7
    hzxxx  
       2022-11-26 16:19:06 +08:00
    v-bind:text='"最后修改时间"+lastModified'
    v-bind:html='"最后修改时间"+lastModified'

    v-bind:text='`最后修改时间+${lastModified}'
    v-bind:html='`最后修改时间+${lastModified}'
    <div>最后修改时间{{lastModified}}</div>
    supertan
        8
    supertan  
       2022-11-26 17:06:17 +08:00
    @hzxxx 你这模板字符串用了个寂寞
    vivipure
        9
    vivipure  
       2022-11-27 00:08:15 +08:00
    从来没用过 v-text ,哈哈
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2703 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 15:37 · PVG 23:37 · LAX 08:37 · JFK 11:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.