简体   繁体   English

在 VueJS 中使用新行显示在表单中输入的文本

[英]Display text as entered in the form with new lines in VueJS

I have a profile filed in the user form as below.我在用户表单中提交了一个配置文件,如下所示。 The issue I am facing here is when I enter text in this field like as below.我在这里面临的问题是当我在此字段中输入文本时,如下所示。

表格中输入的文字

文本如何出现在前端

In the database, the text is saved with new line characters as shown below:在数据库中,文本以换行符保存,如下所示:

My Name is James Scott. I am a Software Developer.\nI live in New York.\nMy favourite programming language is Ruby and Python. 

The issue is the text appears as a single line text.问题是文本显示为单行文本。 There is no new line for I live in New York.我住在纽约,没有新线路。 And My favourite programming language is Ruby and Python.而我最喜欢的编程语言是 Ruby 和 Python。

Please suggest me how I can resolve this issue.请建议我如何解决这个问题。 I am using VueJS on frontend and ruby on backend.我在前端使用 VueJS,在后端使用 ruby。

<v-form :model='user'>
 <v-text-field id="profile"
  ref="profile"
  label="Detailed Profile"
  name="user[profile]"
  v-model='user.profile'
  multi-line
  required
  class="input-element">
 </v-text-field>
</v-form>

user.vue用户.vue

<div id="user-profile">{{user.profile}}</div>

Change the content of the profile inside computed replacing the \n with <br>更改内部profile的内容,将\n computed<br>

computed:{
  profile:function(){
    return this.user.profile.replace(/\n/g, '<br />');
  }
}

and in template you can use it as html content like this在模板中,您可以将其用作html内容,如下所示

<div id="user-profile" v-html="profile"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM