"""Offline Chromium runtime verification for Vue 2 Chapter 18: vue-mixins."""
import json
from playwright.sync_api import sync_playwright
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def make_case(name, expected, actual):
return {"name": name, "expected": expected, "actual": actual, "passed": actual == expected}
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content("""
<!doctype html>
<html><body>
<div id="out"></div>
<script>
const globalMixins = [];
function VueMixinGlobal(m) { globalMixins.push(m); }
function createInstance(options) {
const allMixins = [...globalMixins, ...(options.mixins || [])];
const createdHooks = [];
const methods = {};
const data = {};
for (const m of allMixins) {
if (typeof m.created === 'function') createdHooks.push(m.created);
if (m.data) Object.assign(data, typeof m.data === 'function' ? m.data() : m.data);
if (m.methods) Object.assign(methods, m.methods);
}
if (options.data) Object.assign(data, typeof options.data === 'function' ? options.data() : options.data);
if (options.methods) Object.assign(methods, options.methods);
if (typeof options.created === 'function') createdHooks.push(options.created);
const vm = { $options: options };
Object.assign(vm, data);
for (const [k, fn] of Object.entries(methods)) {
vm[k] = fn.bind(vm);
}
for (const hook of createdHooks) {
hook.call(vm);
}
return vm;
}
// Case 1: basic mixin reuse (Vue.extend pattern)
const log1 = [];
const myMixin = {
created() { this.startmixin(); },
methods: {
startmixin() {
log1.push('欢迎来到混入实例');
document.getElementById('out').textContent = '欢迎来到混入实例';
}
}
};
createInstance({ mixins: [myMixin] });
window.__case1 = { log: log1, domText: document.getElementById('out').textContent };
// Case 2: lifecycle array merge (mixin before component) + shallow data merge (component wins)
const log2 = [];
const hookMixin = {
data() { return { message: 'mixin-msg', foo: 'abc' }; },
created() { log2.push('混入调用'); }
};
const vm2 = createInstance({
mixins: [hookMixin],
data() { return { message: 'component-msg', bar: 'def' }; },
created() { log2.push('组件调用'); }
});
window.__case2 = {
createdOrder: log2,
mergedData: { message: vm2.message, foo: vm2.foo, bar: vm2.bar }
};
// Case 3: methods option merge (component overrides conflicting method name)
const log3 = [];
const methodMixin = {
methods: {
hellworld() { log3.push('HelloWorld 方法'); },
samemethod() { log3.push('Mixin:相同方法名'); }
}
};
const vm3 = createInstance({
mixins: [methodMixin],
methods: {
start() { log3.push('start 方法'); },
samemethod() { log3.push('Main:相同方法名'); }
}
});
vm3.hellworld();
vm3.start();
vm3.samemethod();
window.__case3 = log3;
// Case 4: global mixin affects all subsequent instances
const log4 = [];
let globalTouchCount = 0;
VueMixinGlobal({
created() {
globalTouchCount += 1;
const myOption = this.$options.myOption;
if (myOption) log4.push(myOption);
}
});
createInstance({ myOption: 'hello!' });
createInstance({ other: 123 });
window.__case4 = { globalTouchCount, myOptionLogs: log4 };
</script>
</body></html>
""")
cases.append(make_case(
"basic_mixin_reuse",
{"log": ["欢迎来到混入实例"], "domText": "欢迎来到混入实例"},
page.evaluate("window.__case1"),
))
cases.append(make_case(
"lifecycle_and_data_merge",
{
"createdOrder": ["混入调用", "组件调用"],
"mergedData": {"message": "component-msg", "foo": "abc", "bar": "def"},
},
page.evaluate("window.__case2"),
))
cases.append(make_case(
"methods_conflict_precedence",
["HelloWorld 方法", "start 方法", "Main:相同方法名"],
page.evaluate("window.__case3"),
))
cases.append(make_case(
"global_mixin_custom_option",
{"globalTouchCount": 2, "myOptionLogs": ["hello!"]},
page.evaluate("window.__case4"),
))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/vue2-vue-mixins-lab/explore-vue-mixins.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-vue-mixins.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a789.zip
课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a776.zip