跟随chatgpt用vue写一个圣诞树前端项目

第一步,一个名为 `ChristmasTree.vue` 的文件,并在需要使用圣诞树的组件中导入并使用它。

<template>
  <div class="christmas-tree">
    <div class="tree-top"></div>
    <div class="tree-body">
      <div v-for="(row, index) in treeRows" :key="index" class="tree-row">
        <div v-for="(item, itemIndex) in row" :key="itemIndex" class="tree-item">{{ item }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      treeRows: [
        [' ', ' ', ' ', '🎄', ' ', ' ', ' '],
        [' ', ' ', ' ', '🎄', ' ', ' ', ' '],
        [' ', ' ', '🎄', '🎄', '🎄', ' ', ' '],
        [' ', '🎄', '🎄', '🎄', '🎄', '🎄', ' '],
        ['🎄', '🎄', '🎄', '🎄', '🎄', '🎄', '🎄'],
      ],
    };
  },
};
</script>

<style scoped>
.christmas-tree {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 500px;
  font-size: 30px;
}

.tree-body {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.tree-row {
  display: flex;
  justify-content: center;
}

.tree-item {
  margin: 5px;
}
</style>

第二步:在您的主组件文件(通常是 `App.vue`)中,导入 `ChristmasTree.vue` 组件,并在其中使用 `<ChristmasTree />` 标签。

<template>
  <div id="app">
    <ChristmasTree />
  </div>
</template>
<script>
import ChristmasTree from './components/ChristmasTree.vue';

export default {
  components: {
    ChristmasTree,
  },
};
</script>

<style>
#app {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
</style>

第三步:在入口文件 `main.js` 中使用 `createApp` 方法并挂载主组件到应用程序中。以下为main.js

import { createApp } from 'vue';
import App from './App.vue';
import ChristmasTree from './components/ChristmasTree.vue';

createApp({
  render() {
    return (
      <>
        <ChristmasTree />
        <App />
      </>
    );
  },
}).mount('#app');

 第四步:新建终端,使用以下命令启动开发服务器:npm run serve

npm run serve

 运行成功截图如下

网页端效果如图: 

 

今天也是非常成功的一天!~又离高级工程师进了一步呢~

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值