我正在建设放大器页面,应显示不同的下载网址根据国家。我能够显示/隐藏适当的<a>元素的css通过添加isEU组到<amp-geo>配置。但我想要的是页面上只有一个变量href的链接。我试图通过使用来实现这一点,但我无法使用<amp-state>,尽管它是正确生成的。
在我点击<button on="tap:AMP.setState({foo: 'amp-bind'})">Say "Hello amp-bind"</button>之前,即使是amp文档中的示例也无法工作。然后触发计算,页面显示所有值。在此之前,没有插值/计算的值。
我到底做错了什么?

<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<title>TESTPAGE</title>
<meta name="viewport"
content="width=device-width,minimum-scale=1,initial-scale=1">
<script async
src="https://cdn.ampproject.org/v0.js"></script>
<script async
custom-element="amp-geo"
src="https://cdn.ampproject.org/v0/amp-geo-0.1.js"></script>
<script async
custom-element="amp-bind"
src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<style>
[class*=-only] {
display: none;
}
.amp-geo-group-isEU .eu-only {
display: block;
}
body:not(.amp-geo-group-isEU) .non-eu-only {
display: block;
}
</style>
</head>
<body class="amp-geo-pending">
<amp-state id="stateTest"
class="i-amphtml-element i-amphtml-layout-container"
i-amphtml-layout="container"
hidden=""
aria-hidden="true">
<script type="application/json">
{
"testInitialKey": "initial state value"
}
</script>
</amp-state>
<amp-state id="myCircle"
class="i-amphtml-element i-amphtml-layout-container"
i-amphtml-layout="container"
hidden=""
aria-hidden="true">
<script type="application/json">
{
"radius": "4"
}
</script>
</amp-state>
<amp-geo layout="nodisplay">
<script type="application/json">
{
"AmpBind": true,
"ISOCountryGroups": {
"isEU": ["at", "be", "bg", "bs", "ch", "cy", "cz", "de", "dk", "ee", "es", "fi", "fr", "gb", "gr", "hr", "hu", "ie", "is", "it", "li", "lt", "lu", "lv", "mt", "nl", "no", "pl", "pt", "ro", "se", "si", "sk"]
}
}
</script>
</amp-geo>
<h1 [class]="ampGeo.isEU ? 'isEU' : 'nonEU'"
[text]="'isEU: ' + (ampGeo.isEU ? 'true' : 'false').toUpperCase()"></h1>
<h2 [text]="stateTest.testInitialKey"></h2>
<a class="eu-only"
[href]="'http://google.com/' + ampGeo.ISOCountry == 'cy' ? 'cyprusLink' : 'defaultLink'">LINK_CY</a>
<a class="non-eu-only"
href="http://google.com/?frenchLink">LINK_FR</a>
<hr>
<amp-bind-macro id="circleArea"
arguments="radius"
expression="3.14 * radius * radius"></amp-bind-macro>
<div>
The circle has an area of <span [text]="circleArea(myCircle.radius)">0</span>.
</div>
<hr>
<p [text]="'Hello ' + foo">Hello World</p>
<button on="tap:AMP.setState({foo: 'amp-bind'})">Say "Hello amp-bind"</button>
</body>
</html>
发布于 2019-07-05 05:03:58
如果我理解正确的话,您面临的问题是amp-bind仅根据用户操作进行计算。您必须初始化服务器端的初始值。这就是bind的工作方式。这个想法是为了确保页面速度,并且在页面加载时不需要运行JavaScript。
像这样的代码是很常见的
<div class="foo" [class]="x ? 'foo' : 'bar'">也就是说,初始化类属性服务器端,然后在可能导致状态更改的用户交互时动态更新它。
然而,我可以看到这给AMP Geo带来的问题。服务器是否应该复制该功能以预初始化值?有关示例,请参阅https://github.com/ampproject/amphtml/issues/14637。听起来和你面临的问题是一样的。您可能想要在那里添加注释,以提供反馈,说明为什么您希望添加某种类型的支持。
https://stackoverflow.com/questions/56882857
复制相似问题