Vue.js显示空白区域(换行符)

时间:2016-10-21 13:49:48

标签: javascript vue.js nl2br

如何在vue.js中显示行间距?现在一切都在追逐......

已经尝试过这个:

https://laracasts.com/discuss/channels/vue/vuejs-how-to-return-a-string-with-line-break-from-database

但似乎没什么用。现在尝试3天-_-。

我正在使用Vue.js 1.0和browserify

非常感谢!

- 编辑 -

<template>
<div>
    <bar :title="title"></bar>
    <div class="Row Center">
        <div class="Message Center" v-if="!loading">
            <div class="Message__body" v-if="messages">
                <div class="Message__item__body" v-for="message in messages" v-link="{ name: 'Message', params: { message: message.slug }}">
                    <div class="Message__item__body_content">
                        <p class="Message__title">{{ message.subject }}</p>
                    </div>

                    <div class="Message__item__body_content">
                        <p>Reacties: {{ message.totalReactions }}</p>
                    </div>

                    <div class="Message__item__body_content">
                        <p>Door: {{ message.user.name }} {{ message.user.last_name }}</p>
                    </div>
                </div>

                <pagination :last-page="lastPage" :page="page" :name="Message"></pagination>
                <p v-if="noMessages" class="Collection__none">Er zijn momenteel geen berichten voor het topic {{ topic.name }}.</p>
            </div>
        </div>

        <div class="Loader" v-if="loading">
            <grid-loader :loading="loading" :color="color" :size="size"></grid-loader>
        </div>
    </div>

    <div class="Row center" v-if="!loading && page == 1 && topic">
        <div>
            <button type="submit" class="Btn Btn-main" v-link="{ name: 'NewMessage', params: { topic: topic.slug }}">Nieuw bericht</button>
        </div>
    </div>
</div>
</template>

<script>
import Bar              from '../Shared/Bar.vue';
import Pagination       from '../Shared/Pagination.vue';
import Topic            from '../../Services/Topic/TopicService';
import { GridLoader }   from 'vue-spinner/dist/vue-spinner.min.js';

export default {

    components: { Bar, Pagination, GridLoader },

    data () {
        return {
            title: 'Berichten',
            messages: [],
            topic: null,
            noMessages: false,
            loading: false,
            color: "#002e5b",
            page: 1,
            lastPage: 1,
        }
    },

    route: {
        data ({ to }) {
            this.loading = true;
            this.page = to.query.page || 1;
            Topic.show(this.$route.params.topic, this.page)
                    .then((data) => {
                        this.topic = data.data.topic;
                        if(!data.data.messages.data.length == 0) {
                            this.messages = data.data.messages.data;
                            this.lastPage = data.data.messages.last_page;
                        }
                        else {
                            this.noMessages = true;
                        }

                        this.loading = false;
                    });
        }
    }
}
</script>

当我这样做的时候:

<div class="Message__body__message">
        <p>{{ message.message.split("\n"); }}</p>
    </div>

它只添加逗号。

- 编辑 -

enter image description here

3 个答案:

答案 0 :(得分:19)

分割邮件时,您会获得多个数据项,您应该使用v-for处理这些数据项。

&#13;
&#13;
new Vue({
  el: '#app',
  data: {
    message: `this is a message
it is broken across
several lines
it looks like a poem`
  }
});
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.min.js"></script>
<div id="app">
  <template v-for="line in message.split('\n')">{{line}}<br></template>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:7)

将容器的空白样式设置为前行,如下所示:

<div style="white-space: pre-line;">{{textWithLineBreaks}}</div>

答案 2 :(得分:1)

在使用Vue渲染数据之前,您必须先转换数据。

const lines = stringWithLineBreaks.split('\n')
// then render the lines

如果您分享您正在使用的代码,我可以给出更具体的答案。