Prototype中使用Event和Template

本文提供了两个使用Prototype JavaScript框架的示例。第一个示例展示了如何利用Event.observe方法来监听按钮点击事件,并获取事件的相关信息;第二个示例介绍了如何使用Template创建模板,并通过evaluate方法将对象数组中的数据填充到模板中。

Prototype中使用Event的一个简单的例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>使用Event</title>
		<meta name="author" content="Yeeku.H.Lee" />
		<meta name="website" content="http://www.crazyit.org" />
		<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
	</head>
	<body>
		<table border="1">
			<tr>
				<td>
					<div>
						<input id="ok" type="button" value="click me!" />
					</div>
				</td>
			</tr>
		</table>
		<script src="js/prototype-1.6.0.3.js" type="text/javascript"></script>
		<script type="text/javascript">
			Event.observe("ok", "click", function(event) {
				alert("是否为左键事件:" + event.isLeftClick());
				alert("事件源:" + event.element().value);
				alert("最近的td元素:" + event.findElement("td").innerHTML);
		
			});
		</script>
	</body>
</html>
 Prototype中使用Template的一个简单例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>使用Template</title>
		<meta name="author" content="Yeeku.H.Lee" />
		<meta name="website" content="http://www.crazyit.org" />
		<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
	</head>
	<body>
		<script src="js/prototype-1.6.0.3.js" type="text/javascript"></script>
		<script type="text/javascript">
			objArr = [ {
				book : 'Struts2',
				author : "sss"
			}, {
				book : 'Java',
				author : "jjj"
			}, {
				book : 'Java EE',
				author : "eee"
			} ]
			var template = new Template("书名是#{book} , 作者是#{author}.");
			for ( var i = 0; i < objArr.length; i++) {
				document.writeln(template.evaluate(objArr[i]) + "<br />");
			}
		</script>
	</body>
</html>
 
### Vue 3 中事件总线(Bus)的实现与使用方法 在 Vue 3 中,由于移除了对 `Vue.prototype` 的支持,传统的事件总线实现方式已不再适用。以下是两种推荐的实现方法:使用 `mitt` 库直接通过 Vue 实例创建事件总线。 --- #### 方法一:使用 `mitt` 库实现事件总线 `mitt` 是一个轻量级的事件管理库,适用于 Vue 3 的事件总线实现[^2]。 1. **安装 mitt** 使用 npm 或 yarn 安装 `mitt`: ```bash npm install mitt ``` 2. **配置事件总线** 创建一个事件总线文件,例如 `eventBus.js`: ```javascript // eventBus.js import mitt from 'mitt'; const bus = mitt(); // 创建事件总线实例 export default bus; ``` 3. **发布事件** 在需要触发事件的地方,调用 `emit` 方法: ```javascript // 发布事件 import bus from './eventBus'; bus.emit('custom-event', { message: 'Hello, World!' }); ``` 4. **订阅事件** 在需要监听事件的地方,调用 `on` 方法: ```javascript // 订阅事件 import bus from './eventBus'; bus.on('custom-event', (data) => { console.log(data.message); // 输出 "Hello, World!" }); ``` 5. **取消订阅** 在组件销毁时,调用 `off` 方法以避免内存泄漏: ```javascript // 取消订阅 import bus from './eventBus'; const handler = (data) => { console.log(data.message); }; bus.on('custom-event', handler); // 取消订阅 bus.off('custom-event', handler); ``` --- #### 方法二:使用 Vue 实例作为事件总线 尽管 Vue 3 移除了对 `prototype` 的支持,但仍可以通过创建一个独立的 Vue 实例来实现事件总线[^3]。 1. **创建事件总线** 创建一个事件总线文件,例如 `eventBus.js`: ```javascript // eventBus.js import { createApp } from 'vue'; const app = createApp({}); const bus = app.config.globalProperties.$bus = new Vue(); // 创建事件总线实例 export default bus; ``` 2. **发布事件** 使用 `$emit` 方法发布事件: ```javascript // 发布事件 import bus from './eventBus'; bus.$emit('custom-event', { message: 'Hello, World!' }); ``` 3. **订阅事件** 使用 `$on` 方法订阅事件: ```javascript // 订阅事件 import bus from './eventBus'; bus.$on('custom-event', (data) => { console.log(data.message); // 输出 "Hello, World!" }); ``` 4. **取消订阅** 使用 `$off` 方法取消事件监听: ```javascript // 取消订阅 import bus from './eventBus'; const handler = (data) => { console.log(data.message); }; bus.$on('custom-event', handler); // 取消订阅 bus.$off('custom-event', handler); ``` --- #### 注意事项 - 在 Vue 3 中,推荐使用 `mitt` 库替代原生 Vue 实例作为事件总线,因为其更轻量且更符合现代开发规范[^2]。 - 确保在组件销毁时清理事件监听器,以防止内存泄漏[^4]。 --- ### 示例代码 以下是一个完整的示例,展示如何在 Vue 3 中使用 `mitt` 实现事件总线: ```javascript // eventBus.js import mitt from 'mitt'; const bus = mitt(); export default bus; // ComponentA.vue <template> <button @click="sendEvent">发送事件</button> </template> <script> import bus from './eventBus'; export default { methods: { sendEvent() { bus.emit('custom-event', { message: 'Hello from ComponentA' }); } } }; </script> // ComponentB.vue <template> <div>{{ message }}</div> </template> <script> import bus from './eventBus'; export default { data() { return { message: '' }; }, created() { bus.on('custom-event', this.handleEvent); }, beforeDestroy() { bus.off('custom-event', this.handleEvent); }, methods: { handleEvent(data) { this.message = data.message; } } }; </script> ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值