我正在使用LitElement,并尝试在元素级别加载谷歌字体。
我尝试在connectedCallback
事件中以HTML文本形式返回它,但它不起作用。我无法在get styles()
方法中实现它。
<link...>
语句应该放在代码的哪个位置?
我正在使用LitElement,并尝试在元素级别加载谷歌字体。
我尝试在connectedCallback
事件中以HTML文本形式返回它,但它不起作用。我无法在get styles()
方法中实现它。
<link...>
语句应该放在代码的哪个位置?
您可以轻松地在index.html中导入Google字体:
<link href="https://fonts.googleapis.com/css?family=Kaushan+Script&display=swap" rel="stylesheet">
或者你可以创建一个常见的style.js文件并将其包含进来:
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `<style>
@import url('https://fonts.googleapis.com/css?family=Kaushan+Script');
html,
body {
font-family: 'Roboto', Arial, sans-serif;
height: 100%;
margin: 0;
}
...
</style>`;
document.head.appendChild($_documentContainer.content);
var importDoc = window.document;
var style = importDoc.querySelector('style');
document.head.appendChild(style);
或者:
class MyElement extends LitElement {
render() {
return html`
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Kaushan+Script">
`;
}
}
演示(查看我导入了Kaushan+Script
字体的红色a标签)