Skip to content

<script context="module"> 翻译校准 #18

Open
@Copyes

Description

@Copyes

具有context =“module”属性的<script>的模块在首次解析时运行一次,而不是每个组件实例都运行一次。在此类型的模块中申明的Values可以被普通的<script>标签访问到(包括组件标签)但是相反则无法这么使用。
你可以从此模块中export这些绑定值,并且他们会成为编译过的模块的导出值
你无法export默认值,因为默认导出的是组件本身。

<script context="module">
	let totalComponents = 0;

	// this allows an importer to do e.g.
	// `import Example, { alertTotal } from './Example.svelte'`
	export function alertTotal() {
		alert(totalComponents);
	}
</script>

<script>
	totalComponents += 1;
	console.log(`total number of times this component has been created: ${totalComponents}`);
</script>

@yuwanli @tgxpuisb @dujuncheng @daixinye

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions