Vue.js入门 0xF Render函数(5)实战:留言列表

本文是Vue.js入门系列的第五篇,主要介绍如何利用Render函数实现一个留言列表的功能。通过分析index.html、index.js、input.js、list.js和style.css等文件的内容,我们将了解到如何创建和展示留言,以及如何处理用户输入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>留言列表</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <div id="app" v-cloak style="width:500px;margin:0 auto;">
            <div class="message">
                <!--使用v-model实现username和message双向绑定-->
                <v-input v-model="username"></v-input>
                <v-textarea v-model="message" ref="message"></v-textarea>
                <button @click="handleSend">发布</button>
            </div>
            <list :list="list" @reply="handleReply"></list>
        </div>
        <script src="https://cdn.jsdelivr.net/npm/vue"></script>
        <script src="input.js"></script>
        <script src="list.js"></script>
        <script src="index.js"></script>
    </body>
</html>

index.j

<script setup lang=“ts”> import { ref, onMounted, onBeforeUnmount } from ‘vue’ import * as THREE from ‘three’ import { GLTFLoader } from ‘three/addons/loaders/GLTFLoader.js’ // 新增响应式状态 const isHighlighted = ref(false) const originalMaterials = ref<THREE.Material[]>([]) const canvasRef = ref<HTMLCanvasElement | null>(null) let scene: THREE.Scene let camera: THREE.PerspectiveCamera let renderer: THREE.WebGLRenderer let model: THREE.Group // 模型位置和旋转状态 const position = ref({ x: 0, y: 0, z: 0 }) const rotation = ref({ x: 0, y: 0, z: 0 }) const targetPosition = ref({ x: 0, y: 0, z: 0 }) const targetRotation = ref({ x: 0, y: 0, z: 0 }) const initScene = async () => { if (!canvasRef.value) return // 初始化场景 scene = new THREE.Scene() camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000) renderer = new THREE.WebGLRenderer({ canvas: canvasRef.value, antialias: true }) // 设置渲染器尺寸 const container = canvasRef.value.parentElement const width = container?.clientWidth || window.innerWidth const height = container?.clientHeight || window.innerHeight renderer.setSize(width, height) renderer.setClearColor(0xf0f0f0) // 添加灯光 const ambientLight = new THREE.AmbientLight(0xffffff, 1) scene.add(ambientLight) const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5) directionalLight.position.set(5, 5, 5) scene.add(directionalLight) const axesHelper = new THREE.AxesHelper(100) scene.add(axesHelper) // 加载机械爪模型 const loader = new GLTFLoader() try { const gltf = await loader.loadAsync(‘/src/components/MECHANICAL.glb’) model = gltf.scene model.scale.set(3, 3, 3) model.position.set(1, 1, 3) model.rotation.set(0.5 * 3.14, 0, 0) scene.add(model) // 同步到响应式变量 targetPosition.value = position.value = { x: model.position.x, y: model.position.y, z: model.position.z } targetRotation.value = rotation.value = { x: model.rotation.x, y: model.rotation.y, z: model.rotation.z } } catch (error) { console.error(‘模型加载失败:’, error) } camera.position.x = 1 camera.position.y = 1 camera.position.z = 5 targetRotation.value = { x: model.rotation.x, y: model.rotation.y, z: model.rotation.z } const animate = () => { requestAnimationFrame(animate) renderer.render(scene, camera) } animate() } const generateEnvMap = () => { const canvas = document.createElement(‘canvas’) canvas.width = 256 canvas.height = 256 const context = canvas.getContext(‘2d’)! context.fillStyle = ‘#444444’ context.fillRect(0, 0, 256, 256) return new THREE.CanvasTexture(canvas) } // 修改材质创建方法 const createHighlightMaterial = () => { return new THREE.MeshPhysicalMaterial({ metalness: 0.95, roughness: 0.15, clearcoat: 0.5, envMap: generateEnvMap() }) } // 切换材质的函数 const toggleHighlight = () => { if (!model) return model.traverse((child) => { if (child instanceof THREE.Mesh) { if (isHighlighted.value) { // 保存原始材质 originalMaterials.value.push(child.material) child.material = createHighlightMaterial() } else { // 恢复原始材质 child.material = originalMaterials.value.shift()! } } }) isHighlighted.value = !isHighlighted.value } onMounted(initScene) onBeforeUnmount(() => { renderer.dispose() }) </script> <template> <div class=“page”> <h1>变色演示</h1> <button @click=“toggleHighlight”>{{ isHighlighted ? ‘关闭’ : ‘开启’ }}高光</button> <canvas ref=“canvasRef” name=“canvas”></canvas> </div> </template> <style scoped> </style> 报错Uncaught TypeError: Cannot read properties of undefined (reading ‘visible’) at projectObject (chunk-QXP2ABXY.js?v=66c0966e:30444:33) at animate (Color.vue:98:1)
最新发布
03-08
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值