【问题标题】:Vue.js page is not centralizing all my contents in the pageVue.js 页面没有将我的所有内容集中在页面中
【发布时间】:2022-01-12 04:35:03
【问题描述】:

开发一个我向后端发出请求的 viu.js 页面 100% 运行良好,但我试图在整页和中心对内容进行风格化,但我无法集中它们全部。

我有这个简单的代码:

<template>
  <div class="container">
        <div class="left">
          <img src="../assets/GeeksBay-4.jpg" alt="logo" />
        </div>
        <div class="right">
          <nav>
            <li><a href="#">Login</a></li>
            <li><a href="#">Sign Up</a></li>
          </nav>
        </div>
      <div>
        <h1>GeekCentric</h1>
      </div>
      <div class="post-detail">        
        <form class="submit" @submit.prevent="submit">
          <input class="post" placeholder="Type your favourite topic..." v-model="message"/>
        </form>
      </div>
      <div class="postages list-group list-group-flush border-bottom scrollarea">
        <div class="list-group-item list-group-item-action py-3 lh-tight"
             v-for="message in messages" :key="message"
        >
          <div class="post-details col-10 mb-1 small">{{ message.message }}</div>
        </div>
      </div>
    </div>
</template>

<script>
import {ref, onMounted} from 'vue';
import Pusher from 'pusher-js';

export default {
  name: 'App',
  setup() {
    const post = ref('');
    const messages = ref([]);
    const message = ref('');

    onMounted(() => {
      Pusher.logToConsole = true;


      const pusher = new Pusher('068133b23cfaf634458b', {
        cluster: 'us3'
      });

      const channel = pusher.subscribe('chat');
      channel.bind('message', data => {
        messages.value.push(data);
      });
    });

    const submit = async () => {
      await fetch('http://localhost:8000/api/messages', {
        method: 'POST',
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
          post: post.value,
          message: message.value
        })
      })
      message.value = '';
      post.value = '';
    }

    return {
      post,
      messages,
      message,
      submit
    }
  }
}
</script>

<style scoped>
.container {
  background-color: #330624;
  padding: 10px;
  text-align: center;
  box-sizing: border-box;
}
.left {
  float: left;
  padding-bottom: 25px;
}
.right {
  float: right;
}
h1 {
  color: #fdb924;
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
.submit {
  margin-top: 45px;
  width: 100%;
}
.post-detail {
  margin: 1px;
  flex-direction: row;
}
.post-details {
  background-color: #330624;
  padding: 10px;
  box-sizing: border-box;
  color: #fff;
  font-size: 18px;
  border-bottom: 3px solid #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.postages {
  padding: 50px;
  box-sizing: border-box;  
  color: rgb(88, 88, 88);
  margin: 0 10px;
  border-bottom: 3px solid #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  flex-direction: row;
}
.scrollarea {
  min-height: 808px;
  margin: auto;
}
</style>

也许有一些我看不到的东西,因为我是 vue.js 的新手,所以我在寻求任何人的帮助,或者可能是因为我将引导程序与一些样式代码混合在一起,这有什么意义吗?

【问题讨论】:

    标签: javascript css vue.js styles


    【解决方案1】:

    我在您的徽标、页面标题和导航链接周围设置了一个标题标签。让它们显示为 flex,而不是浮动侧面对象。然后相应地使用您的rightleft 类来text-align 它们。

    此外,在 HTML 代码的其余部分周围插入了一个 section 标签,这样可以更轻松地设计和调试代码。

    稍后,考虑为页面的每个部分创建组件,以便真正利用 Vue.js 框架。

    这应该可以完成工作:

    <template>
      <div class="container">
        <div class="main-bg">
          <header>
            <div class="left">
              <img src="../assets/GeeksBay-4.jpg" alt="logo" />
            </div>
            <div class="geek">
              <h1>GeekCentric</h1>
            </div>
            <div class="right">
              <nav>
                <li><a href="#">Login</a></li>
                <li><a href="#">Sign Up</a></li>
              </nav>
            </div>
          </header>
          <section>
            <div class="post-detail">        
              <form class="submit" @submit.prevent="submit">
                <input class="post" placeholder="Type your favourite topic..." v-model="message"/>
              </form>
            </div>
            <div class="postages list-group list-group-flush border-bottom scrollarea">
              <div class="list-group-item list-group-item-action py-3 lh-tight"
                  v-for="message in messages" :key="message"
              >
                <div class="post-details col-10 mb-1 small">{{ message.message }}</div>
              </div>
            </div>
          </section>
        </div>
      </div>
    </template>
    
    <script>
    import {ref, onMounted} from 'vue';
    import Pusher from 'pusher-js';
    
    export default {
      name: 'App',
      setup() {
        const post = ref('');
        const messages = ref([]);
        const message = ref('');
    
        onMounted(() => {
          Pusher.logToConsole = true;
    
    
          const pusher = new Pusher('068133b23cfaf634458b', {
            cluster: 'us3'
          });
    
          const channel = pusher.subscribe('chat');
          channel.bind('message', data => {
            messages.value.push(data);
          });
        });
    
        const submit = async () => {
          await fetch('http://localhost:8000/api/messages', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
              post: post.value,
              message: message.value
            })
          })
          message.value = '';
          post.value = '';
        }
    
        return {
          post,
          messages,
          message,
          submit
        }
      }
    }
    </script>
    
    <style scoped>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    header {
      margin-bottom: 2vw;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    header div {
      width: 33%;
    }
    .container {
      background-color: #330624;
      padding: 30px;
      text-align: center;
      box-sizing: border-box;
    }
    h1 {
      color: #fdb924;
      font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    }
    .left {
      text-align: left;
    }
    .right {
      text-align: right;
    }
    .submit {
    }
    .post-detail {
    }
    .post-details {
    }
    .postages {
    }
    .scrollarea {
      min-height: 808px;
      margin: auto;
    }
    </style>
    

    注意:我取出了一些尚未使用的类,以便更好地测试它。

    【讨论】:

    • 它使内容向左移动,但我正在尝试使文本、徽标输入和链接集中在该区域中,同时也填充页面中的所有背景,不过还是谢谢
    • 如果您希望徽标和导航链接在其 div 中居中,您可以从 .left.right 类中取出 text-align。要让它们填充 100% 的 div,您可以设置 header div { width: 100%; }
    • 我这样解决了一些问题:在 40px 的右侧添加了一个 margin-top,一个 margin-right: 150px;和一个 margin-top: 40px 在 h1 标签上,将 .scrollarea min-height 更改为 700px;并删除左侧区域的填充底部,但我的页面仍然没有填充我的背景颜色和内容
    猜你喜欢
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多