nuxt-link-DRGY1ko4.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import { defineComponent, ref, h, resolveComponent, computed } from 'file://D:/zwj/nuxt/sannongzixunwang_web/node_modules/vue/index.mjs';
  2. import { parseQuery, hasProtocol, joinURL, withTrailingSlash, withoutTrailingSlash } from 'file://D:/zwj/nuxt/sannongzixunwang_web/node_modules/ufo/dist/index.mjs';
  3. import { u as useRouter, n as nuxtLinkDefaults, r as resolveRouteObject, a as navigateTo, b as useNuxtApp, c as useRuntimeConfig } from './server.mjs';
  4. async function preloadRouteComponents(to, router = useRouter()) {
  5. {
  6. return;
  7. }
  8. }
  9. const firstNonUndefined = (...args) => args.find((arg) => arg !== void 0);
  10. // @__NO_SIDE_EFFECTS__
  11. function defineNuxtLink(options) {
  12. const componentName = options.componentName || "NuxtLink";
  13. function resolveTrailingSlashBehavior(to, resolve) {
  14. if (!to || options.trailingSlash !== "append" && options.trailingSlash !== "remove") {
  15. return to;
  16. }
  17. if (typeof to === "string") {
  18. return applyTrailingSlashBehavior(to, options.trailingSlash);
  19. }
  20. const path = "path" in to && to.path !== void 0 ? to.path : resolve(to).path;
  21. const resolvedPath = {
  22. ...to,
  23. name: void 0,
  24. // named routes would otherwise always override trailing slash behavior
  25. path: applyTrailingSlashBehavior(path, options.trailingSlash)
  26. };
  27. return resolvedPath;
  28. }
  29. function useNuxtLink(props) {
  30. var _a, _b, _c;
  31. const router = useRouter();
  32. const config = useRuntimeConfig();
  33. const hasTarget = computed(() => !!props.target && props.target !== "_self");
  34. const isAbsoluteUrl = computed(() => {
  35. const path = props.to || props.href || "";
  36. return typeof path === "string" && hasProtocol(path, { acceptRelative: true });
  37. });
  38. const builtinRouterLink = resolveComponent("RouterLink");
  39. const useBuiltinLink = builtinRouterLink && typeof builtinRouterLink !== "string" ? builtinRouterLink.useLink : void 0;
  40. const isExternal = computed(() => {
  41. if (props.external) {
  42. return true;
  43. }
  44. const path = props.to || props.href || "";
  45. if (typeof path === "object") {
  46. return false;
  47. }
  48. return path === "" || isAbsoluteUrl.value;
  49. });
  50. const to = computed(() => {
  51. const path = props.to || props.href || "";
  52. if (isExternal.value) {
  53. return path;
  54. }
  55. return resolveTrailingSlashBehavior(path, router.resolve);
  56. });
  57. const link = isExternal.value ? void 0 : useBuiltinLink == null ? void 0 : useBuiltinLink({ ...props, to });
  58. const href = computed(() => {
  59. var _a3;
  60. var _a2;
  61. if (!to.value || isAbsoluteUrl.value) {
  62. return to.value;
  63. }
  64. if (isExternal.value) {
  65. const path = typeof to.value === "object" && "path" in to.value ? resolveRouteObject(to.value) : to.value;
  66. const href2 = typeof path === "object" ? router.resolve(path).href : path;
  67. return resolveTrailingSlashBehavior(
  68. href2,
  69. router.resolve
  70. /* will not be called */
  71. );
  72. }
  73. if (typeof to.value === "object") {
  74. return (_a3 = (_a2 = router.resolve(to.value)) == null ? void 0 : _a2.href) != null ? _a3 : null;
  75. }
  76. return resolveTrailingSlashBehavior(
  77. joinURL(config.app.baseURL, to.value),
  78. router.resolve
  79. /* will not be called */
  80. );
  81. });
  82. return {
  83. to,
  84. hasTarget,
  85. isAbsoluteUrl,
  86. isExternal,
  87. //
  88. href,
  89. isActive: (_a = link == null ? void 0 : link.isActive) != null ? _a : computed(() => to.value === router.currentRoute.value.path),
  90. isExactActive: (_b = link == null ? void 0 : link.isExactActive) != null ? _b : computed(() => to.value === router.currentRoute.value.path),
  91. route: (_c = link == null ? void 0 : link.route) != null ? _c : computed(() => router.resolve(to.value)),
  92. async navigate() {
  93. await navigateTo(href.value, { replace: props.replace, external: isExternal.value || hasTarget.value });
  94. }
  95. };
  96. }
  97. return defineComponent({
  98. name: componentName,
  99. props: {
  100. // Routing
  101. to: {
  102. type: [String, Object],
  103. default: void 0,
  104. required: false
  105. },
  106. href: {
  107. type: [String, Object],
  108. default: void 0,
  109. required: false
  110. },
  111. // Attributes
  112. target: {
  113. type: String,
  114. default: void 0,
  115. required: false
  116. },
  117. rel: {
  118. type: String,
  119. default: void 0,
  120. required: false
  121. },
  122. noRel: {
  123. type: Boolean,
  124. default: void 0,
  125. required: false
  126. },
  127. // Prefetching
  128. prefetch: {
  129. type: Boolean,
  130. default: void 0,
  131. required: false
  132. },
  133. prefetchOn: {
  134. type: [String, Object],
  135. default: void 0,
  136. required: false
  137. },
  138. noPrefetch: {
  139. type: Boolean,
  140. default: void 0,
  141. required: false
  142. },
  143. // Styling
  144. activeClass: {
  145. type: String,
  146. default: void 0,
  147. required: false
  148. },
  149. exactActiveClass: {
  150. type: String,
  151. default: void 0,
  152. required: false
  153. },
  154. prefetchedClass: {
  155. type: String,
  156. default: void 0,
  157. required: false
  158. },
  159. // Vue Router's `<RouterLink>` additional props
  160. replace: {
  161. type: Boolean,
  162. default: void 0,
  163. required: false
  164. },
  165. ariaCurrentValue: {
  166. type: String,
  167. default: void 0,
  168. required: false
  169. },
  170. // Edge cases handling
  171. external: {
  172. type: Boolean,
  173. default: void 0,
  174. required: false
  175. },
  176. // Slot API
  177. custom: {
  178. type: Boolean,
  179. default: void 0,
  180. required: false
  181. }
  182. },
  183. useLink: useNuxtLink,
  184. setup(props, { slots }) {
  185. const router = useRouter();
  186. const { to, href, navigate, isExternal, hasTarget, isAbsoluteUrl } = useNuxtLink(props);
  187. const prefetched = ref(false);
  188. const el = void 0;
  189. const elRef = void 0;
  190. function shouldPrefetch(mode) {
  191. var _a2, _b2;
  192. var _a, _b;
  193. return !prefetched.value && (typeof props.prefetchOn === "string" ? props.prefetchOn === mode : (_a2 = (_a = props.prefetchOn) == null ? void 0 : _a[mode]) != null ? _a2 : (_b = options.prefetchOn) == null ? void 0 : _b[mode]) && ((_b2 = props.prefetch) != null ? _b2 : options.prefetch) !== false && props.noPrefetch !== true && props.target !== "_blank" && !isSlowConnection();
  194. }
  195. async function prefetch(nuxtApp = useNuxtApp()) {
  196. if (prefetched.value) {
  197. return;
  198. }
  199. prefetched.value = true;
  200. const path = typeof to.value === "string" ? to.value : isExternal.value ? resolveRouteObject(to.value) : router.resolve(to.value).fullPath;
  201. await Promise.all([
  202. nuxtApp.hooks.callHook("link:prefetch", path).catch(() => {
  203. }),
  204. !isExternal.value && !hasTarget.value && preloadRouteComponents(to.value, router).catch(() => {
  205. })
  206. ]);
  207. }
  208. return () => {
  209. var _a;
  210. if (!isExternal.value && !hasTarget.value) {
  211. const routerLinkProps = {
  212. ref: elRef,
  213. to: to.value,
  214. activeClass: props.activeClass || options.activeClass,
  215. exactActiveClass: props.exactActiveClass || options.exactActiveClass,
  216. replace: props.replace,
  217. ariaCurrentValue: props.ariaCurrentValue,
  218. custom: props.custom
  219. };
  220. if (!props.custom) {
  221. if (shouldPrefetch("interaction")) {
  222. routerLinkProps.onPointerenter = prefetch.bind(null, void 0);
  223. routerLinkProps.onFocus = prefetch.bind(null, void 0);
  224. }
  225. if (prefetched.value) {
  226. routerLinkProps.class = props.prefetchedClass || options.prefetchedClass;
  227. }
  228. routerLinkProps.rel = props.rel || void 0;
  229. }
  230. return h(
  231. resolveComponent("RouterLink"),
  232. routerLinkProps,
  233. slots.default
  234. );
  235. }
  236. const target = props.target || null;
  237. const rel = firstNonUndefined(
  238. // converts `""` to `null` to prevent the attribute from being added as empty (`rel=""`)
  239. props.noRel ? "" : props.rel,
  240. options.externalRelAttribute,
  241. /*
  242. * A fallback rel of `noopener noreferrer` is applied for external links or links that open in a new tab.
  243. * This solves a reverse tabnapping security flaw in browsers pre-2021 as well as improving privacy.
  244. */
  245. isAbsoluteUrl.value || hasTarget.value ? "noopener noreferrer" : ""
  246. ) || null;
  247. if (props.custom) {
  248. if (!slots.default) {
  249. return null;
  250. }
  251. return slots.default({
  252. href: href.value,
  253. navigate,
  254. prefetch,
  255. get route() {
  256. if (!href.value) {
  257. return void 0;
  258. }
  259. const url = new URL(href.value, "http://localhost");
  260. return {
  261. path: url.pathname,
  262. fullPath: url.pathname,
  263. get query() {
  264. return parseQuery(url.search);
  265. },
  266. hash: url.hash,
  267. params: {},
  268. name: void 0,
  269. matched: [],
  270. redirectedFrom: void 0,
  271. meta: {},
  272. href: href.value
  273. };
  274. },
  275. rel,
  276. target,
  277. isExternal: isExternal.value || hasTarget.value,
  278. isActive: false,
  279. isExactActive: false
  280. });
  281. }
  282. return h("a", { ref: el, href: href.value || null, rel, target }, (_a = slots.default) == null ? void 0 : _a.call(slots));
  283. };
  284. }
  285. });
  286. }
  287. const __nuxt_component_0 = /* @__PURE__ */ defineNuxtLink(nuxtLinkDefaults);
  288. function applyTrailingSlashBehavior(to, trailingSlash) {
  289. const normalizeFn = trailingSlash === "append" ? withTrailingSlash : withoutTrailingSlash;
  290. const hasProtocolDifferentFromHttp = hasProtocol(to) && !to.startsWith("http");
  291. if (hasProtocolDifferentFromHttp) {
  292. return to;
  293. }
  294. return normalizeFn(to, true);
  295. }
  296. function isSlowConnection() {
  297. {
  298. return;
  299. }
  300. }
  301. export { __nuxt_component_0 as _ };
  302. //# sourceMappingURL=nuxt-link-DRGY1ko4.mjs.map